[英]iCloud key-value sync iOS8 Xcode 6
嘗試修復iCloud的某些問題。 這是我的代碼的兩個版本。
- (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始終返回nil。 其他方法不調用。
版本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 sync在iPad上可以正常工作,但在iPhone上無法正常工作。 在iPhone的iCloud Drive設置中,我看到的應用程序與在iPad中的應用程序相同
我的iCloud設置:
會員中心-已啟用iCloud。 兼容性-包括CloudKit支持
目標功能-僅檢查服務的鍵值存儲
默認情況下創建的權利詞典包含具有空數組的鍵com.apple.developer.icloud-container-identifiers和具有正確的appID字符串值的鍵com.apple.developer.ubiquity-kvstore-identifier
那么,為什么iCloudURL總是返回nil? 為什么第二個版本可以在iPad上正常工作,但我的iPhone卻看不到NSUbiquitousKeyValueStoreDidChangeExternallyNotification?
好吧,問題出在升級iOS之后的iPhone中。
從iPhone的設置中刪除iCloud配置文件並再次添加后,iPhone開始與iCloud同步。
現在,該代碼的第二版可在兩種設備上使用。
希望它可以幫助某人解決此類問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.