试图修复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总是返回零。其他方法不调用。
第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设置:
那么,为什么iCloudURL总是返回零呢?以及为什么第二个版本在iPad中正确工作,但是我的iPhone没有看到
发布于 2014-10-20 07:31:56
嗯,问题在我的iPhone升级后的iOS。
当我从iPhone的设置中删除iCloud配置文件并再次添加它时,iPhone就开始与iCloud同步。
现在,第二个版本的代码适用于这两种设备。
希望它能帮助别人解决那种问题。
https://stackoverflow.com/questions/26439901
复制相似问题