2016年6月2日 星期四

iCloud Key-value storage development


The goal is store your app preference to iCloud for backup. That only involved to the key-value storage of iCould.

To use iCloud in an app. You need to configure the in the apple develop like this





Where you can store different type of data like .


Which only key-value storage don't need a container, others are needed a container to store the data in. Look into the apple document would be more helpful.

iCloudDesignGuide

Once you setup the iCould, you could be able to access NSUbiquitous store


    [NSUbiquitousKeyValueStore defaultStore]

and store your key-value data like UserDefault to the ubiquitouskeyvaluestore
  
    [[NSUbiquitousKeyValueStore defaultStore] setObject:obj forKey:key];
    [[NSUbiquitousKeyValueStore defaultStore] synchronize];


and you register a notification of the key
NSUbiquitousKeyValueStoreDidChangeExternallyNotification


And analysis the notification you will get the key-value backup from your iCould account.

    NSDictionary *userInfo = [notificationObject userInfo];
    NSInteger reasonForChangeValue = [[userInfo objectForKey:NSUbiquitousKeyValueStoreChangeReasonKey] integerValue];
        
     if (reasonForChangeValue == NSUbiquitousKeyValueStoreInitialSyncChange)
    {
          // your merge code here
    }


That's it.
You can take look into the
https://github.com/hydrated/iOS-Utilities/tree/master/iCouldSync 


Furthermore you want to make more functionality,
NSUbiquitousKeyValueStoreChangeReasonKey has 4 values can be listening notification. like account change , violate limitation storage.


沒有留言:

張貼留言