未调用 iCloud 键值存储更改 (NSUbiquitousKeyValueStoreDidChangeExternallyNotification)

iCloud Key Value Storage Changes Not Called (NSUbiquitousKeyValueStoreDidChangeExternallyNotification)

NSUbiquitousKeyValueStoreDidChangeExternallyNotification 根本没有被调用吗?

var keyStore: NSUbiquitousKeyValueStore?

viewDidLoad{

NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "ubiquitousKeyValueStoreDidChange:",
            name: NSUbiquitousKeyValueStoreDidChangeExternallyNotification,
            object: keyStore)



userDefinedLabelOne.text = keyStore?.stringForKey("userDefinedStringOne") <---this is changed

 }





func ubiquitousKeyValueStoreDidChange(notification: NSNotification) {

     println("Changed")  <---- Not called


    }

已在此处查看iCloud Key-Value-Store Synchronization issues (NSUbiquitousKeyValueStoreDidChangeExternallyNotification not called)

在 iCloud 功能中选择键值存储

CloudKit 共享数据正常

数据已保存到云端,但未调用更改通知

看看吧。此代码对我有用:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    prepareKeyValueStoreObserver()

    return true
}

func syncKeyValueStore() {
    let kvStore: NSUbiquitousKeyValueStore = NSUbiquitousKeyValueStore.defaultStore();
    kvStore.synchronize()
}

func prepareKeyValueStoreObserver(){
    let kvStore: NSUbiquitousKeyValueStore = NSUbiquitousKeyValueStore.defaultStore();
    let notificationsCenter: NSNotificationCenter = NSNotificationCenter.defaultCenter()
    notificationsCenter.addObserver(self, selector: "ubiquitousKeyValueStoreDidChange:", name: NSUbiquitousKeyValueStoreDidChangeExternallyNotification, object: kvStore)

    syncKeyValueStore()
}

通过退出 iCloud 帐户并重新登录,一切都开始正常工作