[英]iCloud doesn't work first time app is launched
對於我的應用程序,我使用iCloud鍵值存儲來存儲一些用戶設置。 當兩者都安裝了應用程序時,它在我的iPad和iPhone之間完美同步。 我的問題是,當我刪除應用程序,並且我重新運行它時,它沒有iCloud的任何設置,我第一次運行它。 在我再次運行它之后,即使它第一次沒有設置,它也有它們。
我使用了一些NSLog來查看它在鍵值容器中看到的內容,並且第一次應用程序運行時它會顯示“(null)”,但是任何后續運行它都會打印出先前保存的NSArray。
我很樂意提供代碼,但我不完全確定這里的相關內容。
我很感激任何幫助,這個問題讓我發瘋...
添加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");
}
}
}
您的應用程序安裝(和啟動)與KVS下載初始值之間可能存在延遲。 如果您正確注冊了更改通知,您應該會看到值。
您的代碼應始終如下所示,通常在您的-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.