我在結尾處有一個問題。如何顯示AlertView請求許可在應用程序的iCloud生命週期中僅一次種植iCloud?
我從信仰/經驗中學習iCloud不止一次是一個壞主意,而且如果用戶可以做錯事情,他可能遲早會做。
我想要做什麼:
A.當用戶從NO到YES,顯示AlertView要求更改應用程序優先「啓用的iCloud」(是或否)如果用戶希望的種子與現有的雲非iCloud數據。
B.確保應用程序只在iCloud帳戶上種一次iCloud,一旦種子完成第一次就不要放置AlertView。
我的方法:
繼蘋果公司的有關正確使用NSUbiquitousKeyValueStore的,我使用的,下面的方法文檔 - (無效)應用:dFLWOptions:
- (void)updateKVStoreItems:(NSNotification*)notification { // Get the list of keys that changed. NSDictionary* userInfo = [notification userInfo]; NSNumber* reasonForChange = [userInfo objectForKey:NSUbiquitousKeyValueStoreChangeReasonKey]; NSInteger reason = -1; // If a reason could not be determined, do not update anything. if (!reasonForChange) return; // Update only for changes from the server. reason = [reasonForChange integerValue]; if ((reason == NSUbiquitousKeyValueStoreServerChange) || (reason == NSUbiquitousKeyValueStoreInitialSyncChange)) { // 0 || 1 // If something is changing externally, get the changes // and update the corresponding keys locally. NSArray* changedKeys = [userInfo objectForKey:NSUbiquitousKeyValueStoreChangedKeysKey]; NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore]; NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults]; // This loop assumes you are using the same key names in both // the user defaults database and the iCloud key-value store for (NSString* key in changedKeys) {//Only one key: @"iCloudSeeded" a BOOL BOOL bValue = [store boolForKey:key]; id value = [store objectForKey:@"iCloudSeeded"]; [userDefaults setObject:value forKey:key]; } }
}
在應用程序頂部附近包含以下代碼:dFLWO:
NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateKVStoreItems:) name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:store]; // add appDelegate as observer
加載的iCloud存儲後,再與非的iCloud數據種子時它纔會播種從來沒有做過
- (BOOL)loadiCloudStore { if (_iCloudStore) {return YES;} // Don’t load iCloud store if it’s already loaded NSDictionary *options = @{ NSMigratePersistentStoresAutomaticallyOption:@YES ,NSInferMappingModelAutomaticallyOption:@YES ,NSPersistentStoreUbiquitousContentNameKey:@"MainStore" }; NSError *error=nil; _iCloudStore = [_coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self iCloudStoreURL] options:options error:&error]; if (_iCloudStore) { NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore]; BOOL iCloudSeeded = [store boolForKey:@"iCloudSeeded"];//If the key was not found, this method returns NO. if(!iCloudSeeded) // CONTROL IS HERE [self confirmMergeWithiCloud]; // Accept one USER confirmation for seeding in AlertView ONCE world wide return YES; // iCloud store loaded. } NSLog(@"** FAILED to configure the iCloud Store : %@ **", error); return NO; }
一旦播種完成執行以下操作以防止重複接種:將
if (alertView == self.seedAlertView) { if (buttonIndex == alertView.firstOtherButtonIndex) { [self seediCloud]; NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore]; [store setBool:YES forKey:@"iCloudSeeded"]; // NEVER AGAIN //[store synchronize]; } } }
請務必使用上述方法之前得到一個總的iCloud復位:
[NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:[_iCloudStore URL] options:options error:&error])
這是一個非常整潔的解決方案,我的問題,恕我直言,但我不能完全完成它。
我的問題:
如何在第一通知updateKVStoreItems迴應:以上?這是一個錯誤信息的通知。我說這個值是TRUE,但我從來沒有把它設置爲TRUE。如何爲NSUbiquitousKeyValueStore中的鍵設置默認值?
我發現第一個通知是有原因的:NSUbiquitousKeyValueStoreInitialSyncChange 當該筆記進入時,bValue爲YES。這是我的問題。就好像,iCloud/iOS假定任何新的BOOL都是TRUE。 我最初需要此值爲NO,以便我可以繼續並遵循Apple Docs並將NSUserDefault設爲 。再後來,當播種完成後,最終設定值:是爲關鍵:@「iCloudSeeded」
我發現我無法穿透蘋果以下含義:
NSUbiquitousKeyValueStoreInitialSyncChange Your attempt to write to key-value storage was discarded because an initial download from iCloud has not yet happened. That is, before you can first write key-value data, the system must ensure that your app’s local, on-disk cache matches the truth in iCloud. Initial downloads happen the first time a device is connected to an iCloud account, and when a user switches their primary iCloud account.
我不太明白低於2號的影響,這是我在網上查到:
NSUbiquitousKeyValueStoreInitialSyncChange – slightly more complicated, only happens under these circumstances: 1. You start the app and call synchronize 2. Before iOS has chance to pull down the latest values from iCloud you make some changes. 3. iOS gets the changes from iCloud.
如果這個問題是與和NSUserDefaults的不NSUbiquitousKeyValueStore,我相信我會需要去registerDefaults。
我快到了, 請問我該怎麼做! 感謝您的閱讀,馬克