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.





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.