ios - iCloud Key-Value-Store 同步问题(未调用 NSUbiquitousKeyValueStoreDidChangeExternallyNotification)

标签 ios xcode swift ios8 icloud

我正在为 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 功能看起来一切正常:

enter image description here

最佳答案

我遇到了同样的问题。在我的设备上登录 iCloud,然后重新登录,问题就解决了:-)

关于ios - iCloud Key-Value-Store 同步问题(未调用 NSUbiquitousKeyValueStoreDidChangeExternallyNotification),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26572855/

相关文章:

ios - Xamarin,更新 Xcode 后未找到有效的配置文件

ios - 初始化时将数据传递给 UITableViewCell

arrays - Swift 通过在每个索引处获取 max() 从两个 int 数组创建一个 Int 数组

objective-c - API 应该为 int 参数返回 NULL 吗? + 如何处理

ios - UIStepper:如何知道用户点击了步进器的哪个按钮(减号或加号按钮)

iphone - newViewController 始终带有两个 Nib (iPhone 和 iPad)

swift - UISearchController 中的取消按钮在 iOS 13 中没有正确消失

iphone - 原生 iOS 框架将 Wordpress 网站带到 iPhone/Ipad

ios - 在表格 View 中同时使用点击手势和长按

iphone - 我的 AppDelegate 未调用 FBConnect 的 handleOpenURL 方法