One of the common use case when writing an iPhone app is to store user preferences and other application settings such that they are globally accessible within your app and persistent across restarts. While you could use the persistence API for this purpose there is a much simpler solution using NSUserDefaults. NSUserDefaults provides you with access to a persistent global key value store.

Storing Objects

To store an object create an instance of NSUserDefaults with the standardUserDefaults method and then use the setObject method to store your object against a key much as you would do in a NSMutableDictionary. Normally the properties are stored in memory and written to backing store periodically. To force a persistent storage update call the synchronize method.

NSString* message = @"This is the message.";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:message forKey:@"message"];
[defaults synchronize];


Retrieving objects

To retrieve an object from NSUserDefaults just create a instance much like before with and then call the fooForKey method where foo can be any one of; array, bool, data, dictionary, float, integer, object, stringArray, string, double, URL.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString* message = [defaults stringForKey:@"message"];


The last value set for each key will be retrieved across application restarts.