【发布时间】:2014-12-13 21:58:23
【问题描述】:
尝试解决 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 同步适用于 iPad,但不适用于 iPhone。在 iPhone 的 iCloud Drive 设置中,我看到我的应用程序与 iPad 中的一样
我的 iCloud 设置:
会员中心 - 启用 iCloud。兼容性 - 包括 CloudKit 支持
目标功能 - 服务仅检查键值存储
默认创建的权利字典包含键 com.apple.developer.icloud-container-identifiers 和一个空数组和键 com.apple.developer.ubiquity-kvstore-identifier 以及我的 appID 的正确字符串值
那么,为什么 iCloudURL 总是返回 nil?为什么第二个版本可以在 iPad 上正常工作,但我的 iPhone 没有看到 NSUbiquitousKeyValueStoreDidChangeExternallyNotification?
【问题讨论】:
标签: objective-c iphone xcode icloud