Use the iCloud key-value store to make preference, configuration, and app-state data available to every instance of your app on every device connected to a user’s iCloud account. You can store scalar values such as
BOOL, as well as values containing any of the property list object types:
- iOS 5.0+
- macOS 10.7+
- tvOS 5.0+
- watchOS 2.0+
Changes your app writes to the key-value store object are initially held in memory, then written to disk by the system at appropriate times. If you write to the key-value store object when the user is not signed into an iCloud account, the data is stored locally until the next synchronization opportunity. When the user signs into an iCloud account, the system automatically reconciles your local, on-disk keys and values with those on the iCloud server.
Any device running your app, and attached to the same iCloud account, can upload key-value changes to iCloud. To keep track of such changes, register for the
didChangeExternallyNotification notification during app launch. Then, obtain the keys and values from iCloud (which may be newer than those that are local) by calling the
synchronize() method. You need not call the
synchronize() method again during your app’s life cycle, unless your app design requires fast-as-possible upload to iCloud after you change a value.
Avoid using this class for data that is essential to your app’s behavior when offline; instead, store such data directly into the local user defaults database.
The total amount of space available in your app’s key-value store, for a given user, is 1 MB. There is a per-key value size limit of 1 MB, and a maximum of 1024 keys. If you attempt to write data that exceeds these quotas, the write attempt fails and no change is made to your iCloud key-value storage. In this scenario, the system posts the
didChangeExternallyNotification notification with a change reason of
The maximum length for key strings for the iCloud key-value store is 64 bytes using UTF8 encoding. Attempting to write a value to a longer key name results in a runtime error.
To use this class, you must distribute your app through the App Store or Mac App Store, and you must request the
com.apple.developer.ubiquity-kvstore-identifier entitlement in your Xcode project. For more on this, see Configuring Common Key-Value Storage for Multiple Apps in iCloud Design Guide.
This class is not meant to be subclassed.