NSUbiquitousKeyValueStore Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/Foundation.framework
Availability
Available in iOS 5.0 and later.
Declared in
NSUbiquitousKeyValueStore.h
Related sample code

Overview

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: NSNumber, NSString, NSDate, NSData, NSArray, and NSDictionary.

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. 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 NSUbiquitousKeyValueStoreDidChangeExternallyNotification 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.

For more information on adopting key-value storage in your app, see “Designing for Key-Value Data in iCloud” in iCloud Design Guide.

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 NSUbiquitousKeyValueStoreDidChangeExternallyNotification notification with a change reason of NSUbiquitousKeyValueStoreQuotaViolationChange.

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 “Request Access to iCloud Storage By Using Entitlements” in iCloud Design Guide.

This class is not meant to be subclassed.

Tasks

Getting the Shared Instance

Getting Values

Setting Values

Explicitly Synchronizing In-Memory Key-Value Data to Disk

Removing Keys

Retrieving the Current Keys and Values

Class Methods

defaultStore

Returns the shared iCloud key-value store object.

+ (NSUbiquitousKeyValueStore *)defaultStore
Return Value

The shared iCloud key-value store object.

Discussion

An app must always use the default iCloud key-value store object to get and set values. This store is tied to the unique identifier string your app provides in its entitlement requests.

Availability
  • Available in iOS 5.0 and later.
Related Sample Code
Declared In
NSUbiquitousKeyValueStore.h

Instance Methods

arrayForKey:

Returns the array associated with the specified key.

- (NSArray *)arrayForKey:(NSString *)aKey
Parameters
aKey

A key in the key-value store.

Return Value

The array associated with the specified key, or nil if the key was not found or its value is not an NSArray object.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

boolForKey:

Returns the Boolean value associated with the specified key.

- (BOOL)boolForKey:(NSString *)aKey
Parameters
aKey

A key in the key-value store.

Return Value

If a Boolean value is associated with the specified key, that value is returned. If the key was not found, this method returns NO.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

dataForKey:

Returns the data object associated with the specified key.

- (NSData *)dataForKey:(NSString *)aKey
Parameters
aKey

A key in the key-value store.

Return Value

The data object associated with the specified key or nil if the key was not found or its value is not an NSData object.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

dictionaryForKey:

Returns the dictionary object associated with the specified key.

- (NSDictionary *)dictionaryForKey:(NSString *)aKey
Parameters
aKey

A key in the key-value store.

Return Value

The dictionary object associated with the specified key or nil if the key was not found or its value is not an NSDictionary object.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

dictionaryRepresentation

Returns a dictionary containing all of the key-value pairs in the key-value store.

- (NSDictionary *)dictionaryRepresentation
Return Value

A dictionary containing the key and value data in the key-value store.

Discussion

This method returns the in-memory version of the keys and values. If you want to ensure that this dictionary contains the most recent set of changes, call synchronize shortly before calling this method.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

doubleForKey:

Returns the double value associated with the specified key.

- (double)doubleForKey:(NSString *)aKey
Parameters
aKey

A key in the key-value store.

Return Value

The double value associated with the specified key or 0 if the key was not found. If the key exists but does not contain a numerical value, this method returns 0.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

longLongForKey:

Returns the long long value associated with the specified key.

- (long long)longLongForKey:(NSString *)aKey
Parameters
aKey

A key in the key-value store.

Return Value

The long long value associated with the specified key or 0 if the key was not found. If the key exists but does not contain a numerical value, this method returns 0.

Availability
  • Available in iOS 5.0 and later.
Related Sample Code
Declared In
NSUbiquitousKeyValueStore.h

objectForKey:

Returns the object associated with the specified key.

- (id)objectForKey:(NSString *)aKey
Parameters
aKey

A key in the key-value store.

Return Value

The object associated with the specified key or nil if the key was not found.

Discussion

You can use this method to retrieve objects whose type you do not necessarily know from the key-value store. The object returned by this method is always one of the property list types: NSNumber, NSString, NSDate, NSData, NSArray, or NSDictionary.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

removeObjectForKey:

Removes the value associated with the specified key from the key-value store.

- (void)removeObjectForKey:(NSString *)aKey
Parameters
aKey

The key corresponding to the value you want to remove.

Discussion

If the specified key is not found in the key-value store, this method does nothing. This method removes the key from the in-memory version of the store only. Call the synchronize method at appropriate times to update the information on disk.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

setArray:forKey:

Sets an array object for the specified key in the key-value store.

- (void)setArray:(NSArray *)anArray forKey:(NSString *)aKey
Parameters
anArray

An array whose contents can be stored in a property list format. In other words, the objects in the array must be of the types NSNumber, NSString, NSDate, NSData, NSArray, or NSDictionary. The total size (in bytes) of the array and its contents must not exceed the per-key size limits.

aKey

The key under which to store the value. The length of this key must not exceed 64 bytes using UTF8 encoding.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

setBool:forKey:

Sets a Boolean value for the specified key in the key-value store.

- (void)setBool:(BOOL)value forKey:(NSString *)aKey
Parameters
value

The Boolean value to store.

aKey

The key under which to store the value. The length of this key must not exceed 64 bytes using UTF8 encoding.\

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

setData:forKey:

Sets a data object for the specified key in the key-value store.

- (void)setData:(NSData *)aData forKey:(NSString *)aKey
Parameters
aData

The data object to store. The total size of this data object (including any object overhead) must not exceed 1 MB.

aKey

The key under which to store the value. The length of this key must not exceed 64 bytes using UTF8 encoding.

Discussion

Using an NSData object as a value in key-value storage lets you store arbitrary data. For example, in a game app, you can use it to store game state to iCloud.

Be sure to exercise caution when storing a data object. Because it is available to be read and modified by every instance of your app attached to a user’s iCloud account, some of which may be older versions or running on another platform, you must diligently protect a data object’s integrity. For more information, see “Exercise Caution When Using NSData Objects as Values” in iCloud Design Guide.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

setDictionary:forKey:

Sets a dictionary object for the specified key in the key-value store.

- (void)setDictionary:(NSDictionary *)aDictionary forKey:(NSString *)aKey
Parameters
aDictionary

A dictionary whose contents can be stored in a property list format. In other words, the objects in the dictionary must be of the types NSNumber, NSString, NSDate, NSData, NSArray, or NSDictionary. The total size (in bytes) of the dictionary and its contents must not exceed the per-key size limits.

aKey

The key under which to store the value. The length of this key must not exceed 64 bytes using UTF8 encoding.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

setDouble:forKey:

Sets a double value for the specified key in the key-value store.

- (void)setDouble:(double)value forKey:(NSString *)aKey
Parameters
value

The double value to store.

aKey

The key under which to store the value. The length of this key must not exceed 64 bytes using UTF8 encoding.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

setLongLong:forKey:

Sets a long long value for the specified key in the key-value store.

- (void)setLongLong:(long long)value forKey:(NSString *)aKey
Parameters
value

The long long value to store.

aKey

The key under which to store the value. The length of this key must not exceed 64 bytes using UTF8 encoding.

Availability
  • Available in iOS 5.0 and later.
Related Sample Code
Declared In
NSUbiquitousKeyValueStore.h

setObject:forKey:

Sets an object for the specified key in the key-value store.

- (void)setObject:(id)anObject forKey:(NSString *)aKey
Parameters
anObject

The object you want to store. The type of the object must be one of the property list types: NSNumber, NSString, NSDate, NSData, NSArray, or NSDictionary. The total size (in bytes) of the object must not exceed the per-key size limits.

aKey

The key under which to store the value. The length of this key must not exceed 64 bytes using UTF8 encoding.

Discussion

If the type of anObject is not one of the property list types, this method does not set it in the key-value store. Instead, it logs an error and silently ignores the object.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

setString:forKey:

Sets a string object for the specified key in the key-value store.

- (void)setString:(NSString *)aString forKey:(NSString *)aKey
Parameters
aString

The string you want to store. The total size (in bytes) of the string must not exceed the per-key size limits.

aKey

The key under which to store the value. The length of this key must not exceed 64 bytes using UTF8 encoding.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

stringForKey:

Returns the string associated with the specified key.

- (NSString *)stringForKey:(NSString *)aKey
Parameters
aKey

A key in the key-value store.

Return Value

The string associated with the specified key or nil if the key was not found or its value is not an NSString object.

Availability
  • Available in iOS 5.0 and later.
Declared In
NSUbiquitousKeyValueStore.h

synchronize

Explicitly synchronizes in-memory keys and values with those stored on disk.

- (BOOL)synchronize
Return Value

YES if the in-memory and on-disk keys and values were synchronized, or NO if an error occurred. For example, this method returns NO if an app was not built with the proper entitlement requests.

Discussion

The only recommended time to call this method is upon app launch, or upon returning to the foreground, to ensure that the in-memory key-value store representation is up-to-date.

Changes you make to the key-value store are saved to memory. The system then synchronizes the in-memory keys and values with the local on-disk cache, automatically and at appropriate times. For example, it synchronizes the keys when your app is put into the background, when changes are received from iCloud, and when your app makes changes to a key but does not call the synchronize method for several seconds.

This method does not force new keys and values to be written to iCloud. Rather, it lets iCloud know that new keys and values are available to be uploaded. Do not rely on your keys and values being available on other devices immediately. The system controls when those keys and values are uploaded. The frequency of upload requests for key-value storage is limited to several per minute.

During synchronization between memory and disk, this method updates your in-memory set of keys and values with changes previously received from iCloud.

Availability
  • Available in iOS 5.0 and later.
Related Sample Code
Declared In
NSUbiquitousKeyValueStore.h

Constants

Notification Keys

These keys can be included in the user info dictionary of the NSUbiquitousKeyValueStoreDidChangeExternallyNotification notification.

NSString * const NSUbiquitousKeyValueStoreChangeReasonKey;
NSString * const NSUbiquitousKeyValueStoreChangedKeysKey;
Constants
NSUbiquitousKeyValueStoreChangeReasonKey

The value of this key indicates the reason the key-value store changed, as described in “Change Reason Values .”

An NSNumber object with an integer value.

Available in iOS 5.0 and later.

Declared in NSUbiquitousKeyValueStore.h.

NSUbiquitousKeyValueStoreChangedKeysKey

The value of this key is an array of NSString objects, where each is the name of a key that changed in the key-value store.

Available in iOS 5.0 and later.

Declared in NSUbiquitousKeyValueStore.h.

Change Reason Values

Possible values associated with the NSUbiquitousKeyValueStoreChangeReasonKey key.

enum {
   NSUbiquitousKeyValueStoreServerChange,
   NSUbiquitousKeyValueStoreInitialSyncChange,
   NSUbiquitousKeyValueStoreQuotaViolationChange,
   NSUbiquitousKeyValueStoreAccountChange
};
Constants
NSUbiquitousKeyValueStoreServerChange

A value changed in iCloud. This occurs when another device, running another instance of your app and attached to the same iCloud account, uploads a new value.

Available in iOS 5.0 and later.

Declared in NSUbiquitousKeyValueStore.h.

NSUbiquitousKeyValueStoreInitialSyncChange

Your attempt to write to key-value storage was discarded because an initial download from iCloud has not yet happened. That is, before you can first write key-value data, the system must ensure that your app’s local, on-disk cache matches the truth in iCloud.

Initial downloads happen the first time a device is connected to an iCloud account, and when a user switches their primary iCloud account.

Available in iOS 5.0 and later.

Declared in NSUbiquitousKeyValueStore.h.

NSUbiquitousKeyValueStoreQuotaViolationChange

Your app’s key-value store has exceeded its space quota on the iCloud server.

Available in iOS 5.0 and later.

Declared in NSUbiquitousKeyValueStore.h.

NSUbiquitousKeyValueStoreAccountChange

The user has changed the primary iCloud account. The keys and values in the local key-value store have been replaced with those from the new account, regardless of the relative timestamps.

Available in iOS 6.0 and later.

Declared in NSUbiquitousKeyValueStore.h.

Notifications

NSUbiquitousKeyValueStoreDidChangeExternallyNotification

Posted when the value of one or more keys in the local key-value store changed due to incoming data pushed from iCloud.

This notification is sent only upon a change received from iCloud; it is not sent when your app sets a value.

The user info dictionary can contain the reason for the notification as well as a list of which values changed, as follows:

The notification object is the NSUbiquitousKeyValueStore object whose contents changed.

Availability
Declared In
NSUbiquitousKeyValueStore.h