How to use iCloud key value storage

Storing small bits of info in iCloud is very similar to using the local NSUserDefaults device storage. The only difference is that your App ID needs to be enabled for iCloud in iTunes Connect and that you need to enable it in your project:

Screen Shot 2013-01-10 at 19.00.51

Here’s how you save data to iCloud (from a text label):

// save to iCloud
    NSUbiquitousKeyValueStore *cloudStore = [NSUbiquitousKeyValueStore defaultStore];
    [cloudStore setObject:self.myString.text forKey:@"myString"];
    [cloudStore synchronize];

And here’s how to retrieve it:

// load from iCloud
    NSUbiquitousKeyValueStore *cloudStore = [NSUbiquitousKeyValueStore defaultStore];
    self.myString.text = [cloudStore objectForKey:@"myString"];

Synchronization takes a few seconds between devices, provided they are connected to the internet. Note that for testing iCloud you need physical devices – the simulator doesn’t work.

Leave a Reply