iCloud Key-Value Storage in Swift 3

09 August 2017
The iCloud key-value storage is like the UserDefaults but synced across devices. It also survives uninstalls of the app. I used it today to add iCloud backups to Emoji Diary. It required 4 lines of code.

Activate the iCloud capability

Select your project in Xcode and then select the target under "Project and Targets". Activate iCloud and check "Key-Value storage".

Screenshot of project settings in Xcode

Add iCloud to your App ID

  1. Go to https://developer.apple.com/account/ -> Certificates, Identifiers & Profiles.
  2. Under Identifiers -> App IDs, edit your App ID and check the iCloud box. For Key-Value storage "Compatible with Xcode 5" suffices. CloudKit is for documents, large files and Core Data. Click Done.
  3. Go to your Provisioning Profiles. You will see that they turned invalid. For each of them, click edit and then generate to regenerate them. By opening the downloaded Provisioning Profiles, Xcode will automatically update them.

4 lines of code

Wherever you need to access the key-value store, call

    var keyStore = NSUbiquitousKeyValueStore()

Add data just like with the UserDefaults:

    keyStore.set("John Appleseed", forKey: "userName")

Retrieve data somewhere else with:

    let storedUserName = keyStore.string(forKey: "userName")

So far, the keyStore acts like the UserDefaults. The data will be stored locally. To sync them with iCloud, call

    keyStore.synchronize()

This may not synchronize immediately but at "an appropriate later time", as the Apple Docs put it.

Like what you read?

I don't use Google Analytics or Disqus because they require cookies. I would still like to know which posts are popular, so if you liked this post you can let me know here on the right.

You can also leave a comment if you have a GitHub account. The "Sign in" button will store the GitHub API token in a cookie.