Saving Settings using NSUserDefaults Class

Saving simple configuration options can be a chore unless you’ve found the right object. Today we’re going to cover the NSUserDefaults object, which makes saving settings quick and easy. The app we’re going to build as an example is a basic segmented control that will remember which button you’ve selected between launches.

The NSUserDefaults class works a lot like a dictionary. For a given key, you can provide most primitive types and a few object types – NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. The values are stored in a database and are cached to increase performance if they’re being requested often.

The data will automatically be saved back to disk periodically, however you’ll still have to manually invoke synchronize when the application is about to exit.

Adding a UISegmentedControl is pretty basic, so I’ll leave that up to you. What this tutorial will cover is how to update the database when the selection changes and how to set the index when the app loads. First up, we’ll catch the event that occurs when the index changes and save the index.

(IBAction)selectionChanged:(id)sender {
  NSInteger index = ((UISegmentedControl*)sender).selectedSegmentIndex;
 
  // Get the shared defaults object.
  NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
 
  // Save the index.
  [settings setInteger:index forKey:@"MySelectedValueKey"];
 
  // Write them to disk – this is optional here,
  // but should be done when the app exits.
  [settings synchronize];
}

As you can see, it’s pretty simple to save things. I first get the selected index from the UISegmentedControl, then I save it with the key, “MySelectedValueKey”. The key can be anything you want and should probably be set using a #define. The last thing I do is synchronize the database, which will write all changes back to disk. This doesn’t have to be done here, but should at least be done when the application exits, or you may risk losing your setting.

The next thing we need to do is set the selected index when the application launches.

(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
   
  // Override point for customization after application launch.
   
  [self.window makeKeyAndVisible];
 
  // Get the settings and set the selected index.
  NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
  if([settings objectForKey:@"MySelectedValueKey"] != nil) {
    options.selectedSegmentIndex = [settings integerForKey:@"MySelectedValueKey"];
  }
   
  return YES;
}

NSUserDefaults doesn’t have a great way to see if a key exists. Since objectForKey will return nil if there’s nothing there, I like using that method. If something is there, then I use the more specific integerForKey method and set the selected index to the value stored in the database.

And that’s all there is to it. Every time the user launches the app, it will set the selected segment to whatever they last chose. As you can see, the NSUserDefaults object is a great way to store simple settings without all the hassle of file I/O or sqlite access. If you have any questions, feel free to leave them below or check out the forums.

Leave a Reply

Your email address will not be published. Required fields are marked *