Creating Preferences
Many apps include a Preferences pane that appears in the Settings app, as shown in Figure 11.14. Users can access this pane to change basic settings in the app.
Internally, your app can access these preferences by loading a defaults object, like this:
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
Although NSUserDefaults isn’t a subclass of NSDictionary, it holds key-value pairs in the same way. Figure 11.15 shows some of the critical tasks that can be used to get and set values.
11.14 A preferences pane on the iPad. iPhone panes have identical settings but are smaller.
You work with preferences in four steps:
1. Add a file called a Settings Bundle to your app.
2. Edit the file to define the settings you want to include. The file is a plist, and you can use the standard Xcode plist editor.
3. When your app starts, you must register defaults for the preferences. You can then load them into your app wherever they’re needed.
4. You can now read and set keys in your app. If you set a key in your app, its value is saved to the Settings Bundle. The updated value appears in the preferences the next time the user views them.
Adding and editing a Settings Bundle
To add a Settings Bundle, follow these steps:
1. Right-click the Supporting Files group in the Project Navigator.
2. Select New File….
3. Select iOS→Resource and Settings Bundle, ...
Get iOS App Development Portable Genius now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.