I use this tutorials to store object into iCloud (Key-value Storage).
My GUI had only one button that triggers: pressButton
method
Here is a relevant code:
NSString *const keyStorageStr = @"keyForStore";
/* ... */
- (IBAction)pressButton:(UIButton *)sender {
score++;
[DemoiCloudViewController setScore: score];
scoreCountLabel.text = [NSString stringWithFormat:@"%d", score];
// store to preferences
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:score forKey:keyStorageStr];
// store to iCloud
[self setInteger:score forKey:keyStorageStr];
}
- (void)setInteger:(int)value forKey:(NSString*)key {
[self setObject:[NSNumber numberWithInt:value] forKey:key];
}
- (void)setObject:(id)value forKey:(NSString *)key {
NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
if (store) {
[store setObject:value forKey:key];
//[store synchronize];
}
}
And this is my handler:
NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore];
if (store) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(storeChanged:)
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:store];
[store synchronize];
}
- (void)storeChanged:(NSNotification*)notification {/* .. never called .. */}
Here I get proper value: file://localhost/private/var/mobile/Library/Mobile%20Documents/76Z3CR95HF~com~wifi~icloud~WiFi-iCloud-Demo/
- (void)viewDidLoad
{
[super viewDidLoad];
ubiq = [[NSFileManager defaultManager]
URLForUbiquityContainerIdentifier:nil];
if (ubiq) {
NSLog(@"iCloud access at %@", ubiq);
isIcloudActive = true;
} else {
NSLog(@"No iCloud access");
}
When I added handler to listen on everything:
NSNotificationCenter *notifyCenter = [NSNotificationCenter defaultCenter];
[notifyCenter addObserverForName:nil
object:nil
queue:nil
usingBlock:^(NSNotification* notification){
// Explore notification
NSLog(@"Notification found with:"
"\r\n name: %@"
"\r\n object: %@"
"\r\n userInfo: %@",
[notification name],
[notification object],
[notification userInfo]);
}];
I get:
name: _UIApplicationSystemGestureStateChangedNotification
object: (null)
userInfo: (null)
From xCode iCloud stats:
Please help,
Thanks,
What happens if you delete your app and re-run it? Can you retrieve the value from iCloud? Unless there's some other configuration problem that you've not mentioned, my guess is that you can.
The reason: the notification that you're expecting to see only happens when the change comes from iCloud (I.e., originating from another device). The notification will not be triggered when you save a value -- why would it be? You already know the value that has been written!