Problems with iCloud key value synchronization (NSUbiquitousKeyValueStoreDidChangeExternallyNotification is not called)

I am writing a small universal game for iOS. The recorder will be synced across devices through the iCloud Key / Value store.

obtaining the latest grade:

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 } 

maintaining a new record

 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") } } 

for some reason, however, ratings are not synchronized.

  • no mistakes
  • no glitches
  • no null values

I always get the highest score from the current device, never achieved on others.

Could the reason be in itunesconnect or is there something wrong with my code? I use ipad and iphone for testing, both are logged into my icloud account.

I am logging changes for this:

 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 } 

but the function 'icloudKeyValueChanged' is never called.

The features of iCloud are all right, as it seems:

enter image description here

+7
ios xcode ios8 swift icloud
source share
2 answers

I had the same problem. Registering iCloud on my device and logging in to the system solved the problem :-)

0
source share

Probably just adding sync to your record repository:

 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") } } 
-one
source share

All Articles