Reputation: 2995
I am trying to store key-values with NSUbiquitousKeyValueStore. They seem to store fine while the app is installed, but if I delete the app off the device and run the app again, the key-values seem to be lost. Is this expected behaviour, or perhaps is there something I'm missing?
I am storing and retrieving values like so:
-(void)setString:(NSString*)stringValue forKey:(NSString*)keyValue {
NSUbiquitousKeyValueStore *iCloudStore = [NSUbiquitousKeyValueStore defaultStore];
[iCloudStore setString:stringValue forKey:keyValue];
[iCloudStore synchronize];
}
-(NSString*)getStringForKey:(NSString*)keyValue {
NSUbiquitousKeyValueStore *iCloudStore = [NSUbiquitousKeyValueStore defaultStore];
return [iCloudStore stringForKey:keyValue];
}
What I've already checked:
I have registered an observer in the NSNotificationCentre as per Apple docs for NSUbiquitousKeyValueStoreDidChangeExternallyNotification. My observer is never called, on first run after install, or subsequent runs. My code(stripped back from Apple docs)
NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateKVStoreItems:) name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:store];
[store synchronize];
- (void)updateKVStoreItems:(NSNotification*)notification {
NSLog(@"RECEIVED DATA");
}
Additional questions -
NSUbiquitousKeyValueStoreServerChange - A value changed in iCloud. This occurs when another device, running another instance of your app and attached to the same iCloud account, uploads a new value."
Upvotes: 2
Views: 850
Reputation: 122
Did you enable iCloud in the Application Services for the app id in the Apple Developer Portal? After that you also have to create a provisioning profile for the app.
The only difference I see in my code is the "name:" parameter. You need to specify that you want to observe NSUbiquitousKeyValueStoreDidChangeExternallyNotification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector (storeDidChange:)
name: NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object: [NSUbiquitousKeyValueStore defaultStore]];
Your code throws a warning in my environment
Instance method '-addObserver:selector:object:' not found (return type defaults to 'id')
I also have been using setObject:forKey: instead of setString:forKey:
Upvotes: 0