Creating iPad Application Preferences

Creating iPad Application Preferences

Beginning iPad Application Development bookCreating application preferences for your iPad application is a pretty straightforward process. The process involves adding a resource called the Settings Bundle to your project, configuring a property list file, and then deploying your application. When your application is deployed, the application preferences are automatically created for you in the Settings application.

The following Try It Out shows how to add application preferences to your iPad application project in Xcode.

Try It Out Adding Application Preferences

1. Using Xcode, create a new View-based Application project and name it ApplicationSettings.

2. Right-click the project name in Xcode and add a new file. Select the Resource template category and click Settings Bundle (see Figure 8-2). Click Next.

iPad application Resource template category Settings Bundle

Figure 8-2

3. When asked to name the file, use the default name of Settings.bundle and click Finish.

4. The Settings.bundle item should now be part of your project (see Figure 8-3). Click it and view the content of the Root.plist file using the default Property List editor.

iPad Application Root.plist

Figure 8-3

5. Press Command-R to test the application on the iPhone Simulator. When the application is loaded on the Simulator, press the Home key to return to the main screen of the iPad. Tap the Settings application. You can now see a new Settings entry named ApplicationSettings (see Figure 8-4). When you click the ApplicationSettings entry, you see the default settings created for you.

iPad application ApplicationSettings entry

Figure 8-4

How It Works

It seems almost magical that without coding a single line, you have incorporated your application preferences into your application. The magic part is actually the Settings.bundle file that you have added to your project. It contains two files: Root.plist and Root.strings. The Root.plist file is an XML file that contains a collection of dictionary objects (key/value pairs). These key/value pairs are translated into the preferences entries you see in the Settings application.

Take a moment to understand the use of the various keys used in the Root.plist file. There are two root-level keys in the Root.plist file, as follows:

  • StringsTable, which contains the name of the strings file associated with this file. In this case, it is pointing to Root.strings.

  • PreferenceSpecifiers, which is of type Array and contains an array of dictionaries, with each item containing the information for a single preference.

Each preference is represented by an item (known as PreferenceSpecifiers), such as Item 0, Item 1, Item 2, and so on. Each item has a Type key, which indicates the type of data stored. It can be one of the following, as shown in Table 11-1.

Table 11-1: List of Preference Specifiers and Usage 

Element Type
Use For
A text field preference. Displays an optional title and an editable text field.
Preferences that require the user to specify a custom string value.
A read-only string preference.
Displaying preference values as formatted strings.
A toggle switch preference.
Configuring a preference that can have only one of two values.
A slider preference.
Preferences that represent a range of values. The value for this type is a real number whose minimum and maximum you specify.
A multivalue preference.
Preferences that support a set of mutually exclusive values.
A group item preference.
Organizing groups of preferences on a single page.
A child pane preference.
Linking to a new page of preferences.

Each PreferenceSpecifiers key contains a list of subkeys that you can use. For example, the PSTextFieldSpecifier key provides Type, Title, Key, DefaultValue, IsSecure, KeyBoardType, AutocapitalizationType, and AutocorrectionType keys. You then set each key with its appropriate values.

Examine the Root.plist file in more detail Note, for example, that Item 2 has four keys under it: Type, Title, Key, and DefaultValue. The Type key specifies the type of information it is going to store. In this case, it is a PSToggleSwitchSpecifier, which means it will be represented visually as an ON/OFF switch. The Title key specifies the text that will be shown for this item (Item 2). The Key key is the identifier that uniquely identifies this key so that you can programmatically retrieve the value of this item in your application. Finally, the DefaultValue key specifies the default value of this item. In this case, it is checked, indicating that the value is ON.

NOTE: The key/value pair in the Root.plist file is case sensitive. Hence, you need to be careful when modifying the entries. A typo can result in a nonfunctional application.

In the next Try It Out, you modify the Root.plist file so that you can use it to store some user’s credentials. This is very useful in cases where you are writing an application that requires users to log in to a server. When the user uses your application for the first time, he will supply his login credentials, such as username and password. Your application can then store the credentials in the application preferences so that the next time the user uses your application, the application can automatically retrieve the credentials without asking the user to supply them.

NOTE: For more information on the use of each key, refer to Apple’s “Settings Application Schema Reference” documentation. The easiest way to locate it is to do a Web search for the title. The URL is:

Try It Out Modifying the Application Preferences

1. In Xcode, select the Root.plist file and remove all four items under the PreferenceSpecifiers key. You do so by selecting individual items under the PreferenceSpecifiers key and then pressing the Delete key.

2. To add a new item under the PreferenceSpecifiers key, select the PreferenceSpecifiers key and click the Add Child button (see Figure 8-5).

ipad application PreferenceSpecifiers

Figure 8-5

3. A new item is added for you. To add additional items, click the Add Sibling button (see Figure 8-6). Click the Add Sibling button three more times.

iPad application preferences Add Sibling

Figure 8-6

4. The Root.plist file should now look like Figure 8-7.

iPad application preferences Root.plist

Figure 8-7

5. Change the Type of Item 0 to Dictionary and expand it by clicking the arrow displayed to the left of it (see Figure 8-8). Click the Add Child button to add a child to Item 0.

iPad application preferences Add Child

Figure 8-8

6. A new item is added under Item 0 (see Figure 8-9). Click the Add Sibling button to add another item under Item 0.

iPad application preferences Add Sibling

Figure 8-9

Remember, you use the Add Sibling button to add a new item within the same level. Use the Add Child button to add a new child item under the current level.

7. The Root.plist file should now look like Figure 8-10.

iPad Application preferences Root.plist

Figure 8-10

8. Modify the entire Root.plist file so that it looks like Figure 8-11. Ensure that the capitalization of each key and value pair is correct. Pay particular attention to the Type of each item.

iPad application preferences Root.plist

Figure 8-11

9. Save the project and press Command-R to test the application on the iPhone Simulator. Press the Home button and launch the Settings application again. Select the ApplicationSettings settings and observe the preferences shown (see Figure 8-12). Clicking on the Favorite Color setting will display a page for choosing your favorite color (see Figure 8-13).

iPad Application development preferences example

Figure 8-12

iPad Application development preferences example

Figure 8-13

10. Make some changes to the settings values and then press the Home button to return to the Home screen. The changes in the settings are automatically saved to the device. When you return to the Settings page again, the new values will be displayed.

How It Works

What you have done is basically modify the Root.plist file to store three preferences — Login Name, Password, and Favorite Color. For the password field, you use the IsSecure key to indicate that the value must be masked when displaying it to the user. Of particular interest is the Favorite Color preference, for which you use the Titles and Values keys to display a list of selectable choices and their corresponding values to store on the iPad.

The following preference specifiers are used in this example:

  • PSGroupSpecifier — Used to display a group for the settings. In this case, all the settings are grouped under the Account Information group.

  • PSTextFieldSpecifier — Specifies a text field.

  • PSMultiValueSpecifier — Specifies a list of selectable values. The Titles item contains a list of visible text that users can select from. The Values item is the corresponding value for the text selected by the user. For example, if the user selects Blue Color as his favorite color, the value Blue will be stored on the iPhone.

This article is excerpted from chapter 8 "Application Preferences" of the book Beginning iPad Application Development by Wei-Meng Lee (ISBN: 978-0-470-64165-1, Wrox, 2010, Copyright Wiley Publishing Inc.)



One response to “Creating iPad Application Preferences”

  1. Anonymous says:

    In xcode 4.0.2, it does not allow editing the root.plist file. In fact it does not even display it!

Leave a Reply

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