To integrate custom application preferences into the Settings program, you must include a special format of Settings in the top-level directory of the application packagePackageThe preference setting information of the application is provided to the Settings program, while the Settings program displays the preference setting information and writes the user-supplied values to the preference database.
Guidelines for preference settings
Adding preference Settings to the Settings program is most suitable for efficiency tool-type applications and programs that seldom change after the preference value is configured.
Note:Never make preference settings exist on both Setting and Custom Application screens. For example, if a tool-type application has preference Settings on the back of the main view, there should be no configurable Settings in the Settings program. If your application requires preference settings, select and use only one solution.
Preference setting Interface
Each preference setting you display must have a specific type. The preference type defines how the Settings program displays the preference. Most preference setting types are associated with some type of control used for setting, while other types provide a way to organize preference settings. Table 9-1 lists the various element types supported by the Settings program and how to use these types to implement your preferred Settings page.
Table 9-1Type of the preference Element
Element type |
Description |
Text Box |
The text box type displays an optional title and an editable text input box, which is applicable to preference settings that require user-defined string input. The key of this type isPSTextFieldSpecifier. |
Title |
The title TYPE Displays a read-only string, which is applicable to preference settings for displaying read-only strings (if the preference includes implicit or non-direct values, this type can map possible values to strings ). The key of this type isPSTitleValueSpecifier. |
Switch |
The ON/OFF button is displayed for the switch type, which is applicable to the preference setting with two values. This type is usually used to indicate preference settings that contain boolean values, but can also be used to indicate preference settings that contain non-boolean values. The key of this type isPSToggleSwitchSpecifier. |
Slider |
The slider TYPE Displays a slider control, which is applicable to preference settings with a value of a range. The value of this type is a real number. You can specify the minimum and maximum values of the value. The key of this type isPSSliderSpecifier. |
Value List |
The Value List type allows you to select one from a value list. It is applicable to preference settings that support multiple mutex values. These values can be of any type. The key of this type isPSMultiValueSpecifier. |
Group |
The group type allows you to organize several sets of different preferences to one page. The group type does not indicate a configurable preference, but only contains a title string that is displayed before one or more configurable preferences. The key of this type isPSGroupSpecifier. |
Subpage |
The subpage type allows users to access the new preference settings page, which is suitable for multi-level preference settings. For more information about how to configure and use this type, see "multi-level preference settings ". The key of this type isPSChildPaneSpecifier. |
Settings package
In iPhone OS, developers use a special SettingsPackageTo specify the preference settings of the application. This package is namedSettings.bundleResides in the top-level directory of the application package. This package contains one or more Settings page files to define detailed information about application preferences. It can also contain other support files, such as image files or localized files, required for displaying preferences. Table 9-2 lists the contents of a typical Settings package.
Table 9-2
Settings.bundleContents in the directory
Project name |
Description |
Root.plist
|
The Settings page file contains the preference Settings for the root page. Its content is described in more detail in the "Settings page file format" section. |
Others.plistFile |
If you need to build a set of hierarchical preference Settings through multiple sub-panels, the content of each sub-panel is stored in different Settings page files. You are responsible for naming these files and associating them to the correct sub-panel. |
One or more.lprojDirectory |
These directories are used to store the localized string resources of the Settings page files. Each directory contains a string file. The file title is specified on the Settings page. These string files Provide localized content that can be directly displayed to users for preference settings. |
Other images |
If you use the slider control, you can store the slider image in the top-level directory of the package. |
BesidesRoot.plistIn addition to the page file, the Settings program also loads the language resources associated with the file as necessary. Each Settings page file can have an associated.stringsFile that contains the localized values of visible strings. When you prepare to display preference Settings, the Settings program searches for corresponding string resources based on the user preference language and replaces the content on the preference Settings page before display.
Settings page file format
Each Settings page file in the Settings package is set on the iPhoneAttribute list(It is a structured file format) for storage. The simplest way to edit the Settings page file is to use the built-in editor component of Xcode. For details, see "prepare for editing the Settings page; you can also use the property list editor to edit it. It is one of Xcode's tools.
Note:During concatenation, Xcode will automatically convert XML-based Attribute files in the project to binary format. The conversion process is automatically completed during concatenation to save disk space.
The root element of each Settings page file contains the keys listed in Table 9-3. In fact, only one key is required, but we recommend that you include all two keys.
Table 9-3The Root Key in the Settings page file
Key |
Type |
Value |
PreferenceSpecifiers(Must include)
|
Array |
The value of this key isDictionary ArrayEach dictionary in the array contains information about a preference setting element. For a list of element types, see table 9-1. Description of the keys associated with the element type. For more information, seeSettings program structure Reference. |
StringsTable
|
String |
The name of the string file associated with this page file. The project directory of the program that is used in the language package should contain a copy of the string file (with the corresponding localization string ). If you do not include this key, the strings in the page file are not localized. For more information about how to use these strings, see "localized resources. |
Multi-level preference settings if you want to organize preference settings in a certain level, each page you define must have its own
.plistFile, each
.plistThe file contains a set of preference settings that are only displayed on this page. The homepage of application preference settings is always stored in
Root.plistFile, other pages can be named according to your preferences.
To establish a connection between parent and child pages, you must include a child panel element in the parent page. The sub-panel element occupies one row and displays a new Settings page when the user clicks. Sub-Panel ElementFileKey ID.plistFile Name, which defines the content of the subpage;TitleThe title of the Child page. The title is also used as the text of the Child panel element line. The Settings program automatically provides Sub-page roaming control so that users can return to the parent page.
Figure 9-1 shows how a group of multi-level pages work. The left side of the graph shows.plistFile. The relationship between pages is displayed on the right.
Figure 9-1Organize preference settings with sub-panel
Localized Resources
Because preference Settings contain user-visible strings, you should provide localized versions for those strings in the Settings package. For each localized language supported by the package, the preference setting page can have one.stringsFile. When the Settings program encounters a key that supports localization.stringsFind the matched key in the file. If the key is found, the associated value is displayed.
Add and modify Settings packages to access your preference Settings
The program listing 9-1 shows a simple example of how to read preference settings in the application.NSUserDefaultsClass to obtain a preference value created in "configure a Settings page: a tutorial" and assign it to an instance variable of the application.
Program list 9-1Value of the access application preference settings
- (void)applicationDidFinishLaunching:(UIApplication *)application |
{ |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
[self setShouldPlaySounds:[defaults boolForKey:play_sounds_preference]]; |
| |
// Finish app initialization... |
} |
Debug application preference settings in the simulator
When running your application, iPhone Simulator saves all preference values in~/Library/Application Support/iPhone Simulator/User/Applications/<APP_ID>/Library/PreferencesDirectory.<APP_ID>Is a directory name generated by the program. iPhone OS uses it to identify your application.
Every time you reinstall the application, the iPhone OS will perform a clean installation and delete all previous preferences. In other words, connecting to or running an application in Xcode will replace all the content of the old version with the new version. If you want to test the changes in preference settings between two running applications, you must run them directly from the Simulator Interface instead of using Xcode.