简体   繁体   English

iCloud无法在第一次启动应用程序时运行

[英]iCloud doesn't work first time app is launched

For my app, I use iCloud key value storage to store some user settings. 对于我的应用程序,我使用iCloud键值存储来存储一些用户设置。 It syncs perfectly between my iPad and iPhone when both have the app installed. 当两者都安装了应用程序时,它在我的iPad和iPhone之间完美同步。 My issue is that when I delete the app, and I run it fresh, it doesn't have any of the settings from iCloud the FIRST TIME I run it. 我的问题是,当我删除应用程序,并且我重新运行它时,它没有iCloud的任何设置,我第一次运行它。 After I run it again, even though it didn't have the settings the first time, it has them. 在我再次运行它之后,即使它第一次没有设置,它也有它们。

I used some NSLogs to see what it sees in the key-value container, and the first time the app runs fresh it says "(null)", but any subsequent runs it prints out the NSArray that was saved previously. 我使用了一些NSLog来查看它在键值容器中看到的内容,并且第一次应用程序运行时它会显示“(null)”,但是任何后续运行它都会打印出先前保存的NSArray。

I'll gladly provide code, but I'm not entirely sure what is relevant here. 我很乐意提供代码,但我不完全确定这里的相关内容。

I'd appreciate any help, this issue is driving me crazy... 我很感激任何帮助,这个问题让我发疯...

Add an observer for NSUbiquitousKeyValueStoreDidChangeExternallyNotification and sync NSUbiquitousKeyValueStore . 添加NSUbiquitousKeyValueStoreDidChangeExternallyNotification的观察者并同步NSUbiquitousKeyValueStore Wait for the callback to be called soon. 等待很快调用回调。

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");
}

And then use NSUbiquitousKeyValueStoreChangeReasonKey to distinguish between a first time sync and server change sync. 然后使用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");
        }
    }
}

There might be a delay between your app being installed (and launched) and KVS downloading the initial values. 您的应用程序安装(和启动)与KVS下载初始值之间可能存在延迟。 If you properly register to the change notification, you should see the values coming in. 如果您正确注册了更改通知,您应该会看到值。

Your code should always look like this, generally in your -applicationDidFinishLaunching: delegate method: 您的代码应始终如下所示,通常在您的-applicationDidFinishLaunching: delegate方法中:

_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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM