我正在为 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 功能看起来一切正常:
最佳答案
我遇到了同样的问题。在我的设备上登录 iCloud,然后重新登录,问题就解决了:-)
关于ios - iCloud Key-Value-Store 同步问题(未调用 NSUbiquitousKeyValueStoreDidChangeExternallyNotification),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26572855/