Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NSUbiquitousKeyValueStore and NSUserDefaults together

Documentation is not clear on how to use NSUbiquitousKeyValueStore with edge cases.

If I want to set a value, I understand that I should set a value to both NSUserDefaults and NSUbiquitousKeyValueStore since iCloud could be disabled. However in my tests [NSUbiquitousKeyValueStore defaultStore] return a valid object even if iCloud is disabled (tested on Mac OS).

Also, to my understanding is that if iCloud is enabled, NSUbiquitousKeyValueStore's values are stored to disk (and available offline). What are the reason to use NSUserDefaults if you are sure that you have less than 64KB of data?

like image 780
gcamp Avatar asked Nov 09 '11 19:11

gcamp


2 Answers

I am using http://blog.mugunthkumar.com/coding/ios-code-mkicloudsync-sync-your-nsuserdefaults-to-icloud-with-a-single-line-of-code/

It is a simple class written by Mugunth Kumar (thanx !) that does the work for you... Unless you have special needs, add one line of code and it will do all the reading and writing to iCloud... all you need to do is read and write to NSUserDefaults as usual...

Edit:

Carful, if you remove an item from NSUserDefaults the code I linked to above will not remove the item from the cloud. Whenever you remove an item from NSUSerDefaults please do the same to NSUbiquitousKeyValueStore as So:

NSUbiquitousKeyValueStore* keyValueStore=[NSUbiquitousKeyValueStore defaultStore];
[keyValueStore removeObjectForKey: yourKey];
like image 161
elad s Avatar answered Oct 19 '22 23:10

elad s


The Mugunth Kumar answer provided above works beautifully if you want to sync all of your NSUserDefaults!

However, it is an ALL or NOTHING approach. You can not pick or choose the defaults you wish to sync.

I found this Tutorial that may be of assistance if you are looking to be more picky.

like image 40
Moomio Avatar answered Oct 19 '22 23:10

Moomio