Brian Love
Angular + TypeScript Developer in Denver, CO


Reading time ~2 minutes

NSUserDefaults is the Objective-C class that allows you to store and retrieve user preferences for your application. Stored as NSData associated with a string key, the defaults are persisted to a local database on your user’s OS.

For performance reasons, these are placed in memory, and occassionally synchronized back to the database. To get started with NSUserDefaults, you should first register the default values for each setting the user can control.

You can then read or write to the NSUserDefaults class using the objectForKey: and the setObject:forKey: methods. You can read more about the NSUserDefaults class under the developer documentation: NSUserDefaults Class Reference page, and the Preferences and Settings Programming Guide.

OK, let’s start be setting the default user preferences. To do this, we will create an appDefaults NSMutableDictionary instance, and assign objects that we want to store.

Then, we are going to store a NSNumber that is the time the application was last updated. The NSNumber is stored as a unix timestamp, or the number of seconds since the epoch.

Lastly, we are going to get access to the shared NSUserDefaults instance object calling the class method standardUserDefaults on the NSUserDefaults class. Once we have access to the shared instance, we will send the registerDefaults: message to the object(or call the registerDefaults: instance method).

-(void)setDefaultUserPreferences {
  //create dictionary to hold defaults
  NSMutableDictionary *appDefaults = [NSMutableDictionary dictionary];

  //add lastUpdated default object value
  NSDate *now = [NSDate date];
  NSNumber *lastUpdated = [NSNumber numberWithDouble:[now timeIntervalSince1970]];
  [appDefaults setObject:lastUpdated forKey:@"lastUpdated"];

  //register the application defaults
  [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];

You will want to call this setDefaultUserPreferences method when you application launches, to be sure that the defaults are registered after the application is installed. To do this, it is easiest to invoke the setDefaultUserPreferences method from the AppDelegate’s application:didFinishLaunchingWithOptions: method. It is also very common to store your application’s default user preferences in a plist file, especially if there are a lot of them.

We can then access the value of this settings by calling the objectForKey: instance method:

NSNumber *lastUpdated = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastUpdated"];

In the code above, we have defined a NSNumber object pointer named lastUpdated, which has the value of our lastUpdated user preference that we registered (or stored) previously. If we want to update the value stored in our settings database, we simply call the setObject:forKey: method:

NSDate *now = [NSDate date];
[[NSUserDefaults standardUserDefaults] setObject:now forKey:@"lastUpdated"];

It’s that simple. The one thing that seems to catch people is when you try to store a simple C data type for a user preference. Remember that the NSUserDefaults class can only store Objective-C objects. So, if you have a standard C integer, you must first create a NSNumber from that integer before you can save it as a user default. In this example, you would simply create a new NSNumber using the class method numberWithInt:.

Brian Love

Hi, I'm Brian. I am interested in TypeScript, Angular and Node.js. I'm married to my best friend Bonnie, I live in Denver and I ski (a lot).