ios – iCloud键值存储同步问题(未调用NSUbiquitousKeyValueStoreDidChangeExternallyNotification)

我正在为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 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...