iCloud Key-Value Storage in Swift 3 / iOS 10 / Xcode 8

09 August 2017
app-dev
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".

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.

Comments