Tag Archives: NSUbiquitousKeyValueStore

iCloud in your iOS Apps – Part 2: Key/Value Storage

In this part I’ll show you how to store and retrieve data using the NSUbiquitousKeyValueStore singleton, and how to receive the relevant notification so that your app can react if data has changed in iCloud.

Watch the whole series

Enjoy!

Watch the full course in one convenient playlist:
Catch this episode on my iOS Dev Diary Podcast:

How to test the size of an NSDictionary (in bytes)

The NSDictionary class doesn’t have a length property that can tell us how much memory is being used for storing the whole lot. Usually we don’t really care how big our variables grow – but if you’re storing that dictionary somewhere with potentially limited space (such as iCloud Key/Value storage), it may well be of interest.

You can however turn the dictionary into data and test its size like this:

Here we setup a loop that generates a dictionary and adds 1001 entries with an NSNumber literal of 47. This will give us the following information:

Size in bytes: 12954 – Entries: 1001

Note that the above only works if your dictionary has “serialisable” data such as NSNumber, NSDate, NSArray, NSDictionary, NSString or NSData (anything that can be written to a .plist file basically) – but it will not work with custom objects.

How to detect changes in iCloud Key/Value Data

Now that we can read from and write to iCloud, how will we know when another device may have written new data?

We’ll do this by adding an observer and listening to a special message iCloud transmits every time new data is available. The message has the catchy title NSUbiquitousKeyValueStoreDidChangeExternallyNotification.

Here’s how we can setup such an observer. This calls a method (newCloudData) in which you can react to the changes:

How to use iCloud to store Key/Value Data

iCloud-IconUsing iCloud as Key/Value storage is almost identical to NSUserDefaults, except it populates to iCloud in the background and can be read from any device on the same account. What I find much harder than the code is how to make this work in Xcode.

Here’s what we need:

  • an App ID that has iCloud enabled
  • a Provisioning Profile for this App ID
  • an entitlements file, created by Xcode
  • code to save data
  • code to retrieve data

The Code

Let’s start with the easy bit. To store and retrieve key/value data in iCloud we use the

This content is for members only.

NSUbiquitousKeyValueStore class, just like we’d use the NSUserDefaults class for local storage. Usage is identical:

Once setup, we can store values like this:

and retrieve data like that:

Continue reading