【问题标题】:iCloud doesn't work first time app is launched首次启动应用程序时 iCloud 不工作
【发布时间】:2012-08-13 06:08:12
【问题描述】:

对于我的应用,我使用 iCloud 键值存储来存储一些用户设置。当我的 iPad 和 iPhone 都安装了应用程序时,它可以在我的 iPad 和 iPhone 之间完美同步。我的问题是,当我删除应用程序并重新运行它时,它在我第一次运行它时没有任何来自 iCloud 的设置。再次运行后,虽然第一次没有设置,但还是有的。

我使用了一些 NSLogs 来查看它在键值容器中看到的内容,当应用程序第一次运行时它会显示“(null)”,但任何后续运行它都会打印出之前保存的 NSArray。

我很乐意提供代码,但我不完全确定这里的相关内容。

我会很感激任何帮助,这个问题让我发疯......

【问题讨论】:

    标签: objective-c ios cocoa-touch icloud


    【解决方案1】:

    NSUbiquitousKeyValueStoreDidChangeExternallyNotification 添加一个观察者并同步NSUbiquitousKeyValueStore。等待回调很快被调用。

    if([[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil])
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyValueStoreChanged:)
                                                     name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
                                                   object:[NSUbiquitousKeyValueStore defaultStore]];
    
        [[NSUbiquitousKeyValueStore defaultStore] synchronize];
    }
    else
    {
            NSLog(@"iCloud is not enabled");
    }
    

    然后使用NSUbiquitousKeyValueStoreChangeReasonKey区分第一次同步和服务器更改同步。

    -(void)keyValueStoreChanged:(NSNotification*)notification 
    {
        NSLog(@"keyValueStoreChanged");
    
        NSNumber *reason = [[notification userInfo] objectForKey:NSUbiquitousKeyValueStoreChangeReasonKey];
    
        if (reason) 
        {
            NSInteger reasonValue = [reason integerValue];
            NSLog(@"keyValueStoreChanged with reason %d", reasonValue);
    
            if (reasonValue == NSUbiquitousKeyValueStoreInitialSyncChange)
            {
                NSLog(@"Initial sync");
            }
            else if (reasonValue == NSUbiquitousKeyValueStoreServerChange)
            {
                NSLog(@"Server change sync");
            }
            else
            {
                NSLog(@"Another reason");
            }
        }
    }
    

    【讨论】:

    • 好奇...为什么要在 NSFileManager 上使用 -URLForUbiquityContainerIdentifier: 方法?
    • 检查设备上是否启用了iCloud。
    • 注意:由于所有其他原因,总是有一个后备。这是非常重要的。您可以像进行正常的服务器更改一样考虑所有其他未处理的原因。
    • 最好的办法是使用switch 语句并使用default: 来处理NSUbiquitousKeyValueStoreServerChange 和所有其他情况。
    • @erkanyildiz 我明白了,直到现在才注意到这在阅读文档时可能会有所帮助。
    【解决方案2】:

    在您的应用安装(和启动)与 KVS 下载初始值之间可能存在延迟。如果您正确注册到更改通知,您应该会看到输入的值。

    您的代码应始终如下所示,通常在您的 -applicationDidFinishLaunching: 委托方法中:

    _store = [[NSUbiquitousKeyValueStore defaultStore] retain]; // this is the store we will be using
    // watch for any change of the store
    [[NSNotificationCenter defaultCenter] addObserver:self
          selector:@selector(updateKVStoreItems:)
          name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
          object:_store];
    // make sure to deliver any change that might have happened while the app was not launched now
    [_store synchronize];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      • 2020-09-07
      • 1970-01-01
      相关资源
      最近更新 更多