試圖解決與iCloud有關的問題。這裏是我的代碼的兩個版本。iCloud鍵值同步iOS8 Xcode 6
- (void)viewDidLoad{
[super viewDidLoad];
[self checkICloudData];
}
版本1
- (void)checkICloudData
{
NSFileManager * fileManager=[NSFileManager defaultManager];
NSURL *iCloudURL=[fileManager URLForUbiquityContainerIdentifier:nil];
NSLog(@"iCloud URL is %@",[iCloudURL absoluteString]);
if (iCloudURL){
NSUbiquitousKeyValueStore * store=[NSUbiquitousKeyValueStore defaultStore];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateICloudData:)
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:store];
[store synchronize];
}else{
NSLog(@"iCloud is not supported or enabled");
[self loadDataFromBundle];
}
}
iCloudURL總是返回零。其他方法不會調用。
版本2
- (void)checkICloudData
{
NSUbiquitousKeyValueStore * store=[NSUbiquitousKeyValueStore defaultStore];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateICloudData:)
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:store];
[store synchronize];
}
- (void)updateICloudData:(NSNotification*)notification
{
NSDictionary *userInfo = [notification userInfo];
NSNumber *changeReason = [userInfo objectForKey:NSUbiquitousKeyValueStoreChangeReasonKey];
NSInteger reason = -1;
if (!changeReason) {
return;
} else {
reason = [changeReason integerValue];
}
if ((reason == NSUbiquitousKeyValueStoreServerChange) || (reason == NSUbiquitousKeyValueStoreInitialSyncChange)) {
NSArray *changedKeys = [userInfo objectForKey:NSUbiquitousKeyValueStoreChangedKeysKey];
for (NSString *key in changedKeys) {
if ([key isEqualToString:casesKey]) {
[self iCloudNotification];
}else {
[self loadDataFromBundle];
}
}
}
}
與該版本的iCloud同步正常工作與iPad,但不與iPhone的合作。在iPhone的icloud的驅動設置,我看到我的應用程序一樣,在iPad上
我的iCloud設置:
會員中心 - iCloud的啓用。兼容性 - 包括CloudKit支持
目標功能 - 服務只檢查鍵 - 值存儲
默認創建的權利字典有關鍵com.apple.developer.icloud容器的標識符與空數組和關鍵COM .apple.developer.ubiquity-kvstore標識符與我的appid的正確字符串值
那麼,爲什麼iCloudURL總是返回零?以及爲什麼第二個版本在iPad上正常工作,但是我的iPhone沒有看到NSUbiquitousKeyValueStoreDidChangeExternallyNotification?