【问题标题】:iCloud key-value sync iOS8 Xcode 6iCloud 键值同步 iOS8 Xcode 6
【发布时间】: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 设置:

  1. 会员中心 - 启用 iCloud。兼容性 - 包括 CloudKit 支持

  2. 目标功能 - 服务仅检查键值存储

  3. 默认创建的权利字典包含键 com.apple.developer.icloud-container-identifiers 和一个空数组和键 com.apple.developer.ubiquity-kvstore-identifier 以及我的 appID 的正确字符串值

那么,为什么 iCloudURL 总是返回 nil?为什么第二个版本可以在 iPad 上正常工作,但我的 iPhone 没有看到 NSUbiquitousKeyValueStoreDidChangeExternallyNotification?

【问题讨论】:

    标签: objective-c iphone xcode icloud


    【解决方案1】:

    嗯,问题出在我升级 iOS 后的 iPhone 上。

    当我从 iPhone 的设置中删除 iCloud 配置文件并重新添加后,iPhone 开始与 iCloud 同步。

    现在代码的第二个版本适用于两种设备。

    希望它可以帮助某人解决此类问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-12
      • 2014-12-21
      • 2016-08-11
      • 1970-01-01
      • 2015-05-19
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多