How to Set Up User Preference Settings in Your iPad App - dummies

How to Set Up User Preference Settings in Your iPad App

By Neal Goldstein, Tony Bove

You can set preferences for iPad applications, which are application-specific settings used to configure the behavior or appearance of your iPad app. You can create and save preference settings in your app, but you can also use the supplied Settings app to display and set your app-specific preferences (the Settings app icon looks like a bunch of gears). Whatever separate settings feature you come up with has to function within the framework of the Settings app; in effect, the Settings app makes you color within the lines.

Here’s a short summary of the guidelines that the iPad imposes for preference settings:

  • If you have preference settings that are typically configured once and then rarely changed: Leave the task of setting preferences to the Settings app. On an iPad, this applies to things such as enabling/disabling Wi-Fi access, setting wallpaper displays, setting up Mail accounts, and any other preference settings you would set and then leave in place for a while.

  • If you have preference settings that the user might want to change regularly: In this situation, you should consider having users set the options themselves in your app.

To save and read preference settings, you use a built-in, easy-to-use class called NSUserDefaults.

By the way, don’t ask why the language experts put Defaults in the name rather than something to do with preference settings — fewer letters, maybe — but that’s the way it is. Just don’t let their naming idiosyncrasies confuse you.

Storing the data in the file system, rather than in memory, gives you an easy way to store application-specific information. With the help of NSUserDefaults, you can easily store the state the user was in when he or she quit the application — or store something simple like a text string.

Identifying preference settings for NSUserDefaults

It’s really easy to both access and update a preference. NSUserDefaults is implemented as a singleton, meaning there’s only one instance of NSUserDefaults running in your application. To access that one instance, you invoke the class method standardUserDefaults:

 [NSUserDefaults  standardUserDefaults]

standardUserDefaults returns the NSUserDefaults object. As soon as you have access to the standard user defaults, you can store data there and then get it back when you need it. To store data, you simply give it a key and tell it to save the data using that key.

The way you tell it to save something is by using the setObject:forKey: method. (In case your knowledge of Objective-C is a little rusty — or not there at all — that’s the way any message that has two arguments is referred to):

  • The first argument, setObject:, is the object you want NSUserDefaults to save. This object must be NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary.

  • The second argument is forKey:. In order to get the data back, and in order for NSUserDefaults to know where to save it, you have to be able to identify it to NSUserDefaults. You can, after all, have a number of preferences stored in the NSUserDefaults database, and the key tells NSUserDefaults which one you’re interested in.

Reading preferences into the app

To use the preference settings for the app’s view, you link it up with the view controller. The best place to do that is viewDidLoad, which is invoked right after the view has been loaded from the nib file.

After you add the code to use the preference settings, you need to now decide how to enable the user to change these settings. One easy way for your app to offer the preference settings is in a modal dialog box, which the user can use to enter his or her preferences.