我正在为iOS编写一款小型通用游戏。 高分将通过iCloud Key / Value商店在设备上同步。
获得最新分数:
func retrieveHighestScore() -> Int64 {
let iCloudScore: Int64 = NSUbiquitousKeyValueStore.defaultStore().longLongForKey(KeyValueKeyClassification.KeyHighscore.toRaw())
let localScore: Int64 = Int64(NSUserDefaults.standardUserDefaults().integerForKey(KeyValueKeyClassification.KeyHighscore.toRaw()))
var result: Int64 = 0
if localScore > iCloudScore {
storeNewHighscore(localScore)
result = localScore
} else {
storeNewHighscore(iCloudScore)
result = iCloudScore
}
return result
}
存储新的高分
func storeNewHighscore(newScore: Int64) {
NSUbiquitousKeyValueStore.defaultStore().setLongLong(newScore, forKey: KeyValueKeyClassification.KeyHighscore.toRaw())
NSUserDefaults.standardUserDefaults().setInteger(Int(newScore), forKey: KeyValueKeyClassification.KeyHighscore.toRaw())
if NSUbiquitousKeyValueStore.defaultStore().synchronize() {
println("Synched Successfully")
}
}
但出于某种原因,分数未同步。
我总是从当前设备获得最高分,而不是在其他设备上获得的分数。
原因可能在于itunesconnect还是我的代码有问题?我正在使用ipad和iphone进行测试,两者都登录到我自己的icloud帐户。
我正在注册这样的更改:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "icloudKeyValueChanged", name: NSUbiquitousKeyValueStoreDidChangeExternallyNotification, object: nil)
if NSUbiquitousKeyValueStore.defaultStore().synchronize() {
println("initial Synched Successfully")
}
return true
}
但功能' icloudKeyValueChanged'永远不会被称为。
iCloud功能一切正常:
答案 0 :(得分:0)
我遇到了同样的问题。在我的设备上记录iCloud并重新登录,解决了这个问题: - )
答案 1 :(得分:-1)
它可能只是简单地将同步添加到你的高分储存器:
func storeNewHighscore(newScore: Int64) {
NSUbiquitousKeyValueStore.defaultStore().setLongLong(newScore, forKey: KeyValueKeyClassification.KeyHighscore.toRaw())
NSUserDefaults.standardUserDefaults().setInteger(Int(newScore), forKey: KeyValueKeyClassification.KeyHighscore.toRaw())
NSUserDefaults.standardUserDefaults().synchronize()
if NSUbiquitousKeyValueStore.defaultStore().synchronize() {
println("Synched Successfully")
}
}