NSUbiquitousKeyValueStoreDidChangeExternallyNotification
为和同步添加一个观察者 NSUbiquitousKeyValueStore
。等待回调很快被调用。
if([[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil])
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyValueStoreChanged:)
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:[NSUbiquitousKeyValueStore defaultStore]];
[[NSUbiquitousKeyValueStore defaultStore] synchronize];
}
else
{
NSLog(@"iCloud is not enabled");
}
然后NSUbiquitousKeyValueStoreChangeReasonKey
用来区分第一次同步和服务器更改同步。
-(void)keyValueStoreChanged:(NSNotification*)notification
{
NSLog(@"keyValueStoreChanged");
NSNumber *reason = [[notification userInfo] objectForKey:NSUbiquitousKeyValueStoreChangeReasonKey];
if (reason)
{
NSInteger reasonValue = [reason integerValue];
NSLog(@"keyValueStoreChanged with reason %d", reasonValue);
if (reasonValue == NSUbiquitousKeyValueStoreInitialSyncChange)
{
NSLog(@"Initial sync");
}
else if (reasonValue == NSUbiquitousKeyValueStoreServerChange)
{
NSLog(@"Server change sync");
}
else
{
NSLog(@"Another reason");
}
}
}