Preferences

Preferences are settings that an application defines for itself (often as defaults) or that users select for the application. For example, a text editor might have settings for default font, automatic spell-checking, and autosaving frequency. Preferences are recorded in a repository of default or user-specified known as the user defaults (or preferences) system. Your application writes user preferences, in the form of key-value pairs, to the user defaults system and reads preferences from it. Values must be property-list objects. You write and read user preferences to and from the user defaults system by calling methods of the NSUserDefaults class or functions of the Preference Utilities (CFPreference). Preferences are not necessarily user configurable; for example, an application can use the user defaults system to store state information.

Note: Although items in the user defaults system must be property-list objects, you should only use NSUserDefaults methods or CFPreference functions to read and write them. Do not use any other API that deals with property lists such as the NSPropertyListSerialization class.

Each User Preference Belongs to a Domain and a User

When you write a user preference to the user defaults system, it puts it in a domain that reflects the scope of the preference. For example, there’s a domain for application-specific defaults and another for defaults that apply to all applications (NSGlobalDomain). Although preferences in these domains are persistent across application launches, preferences in some domains exist only while an application is running.

User preferences are also associated with a particular user. Thus you can look at a preference “record” in the user-defaults repository as being structured in three levels: first user, then domain (usually global or per-application), and then key-value pairs within each domain. The root object for the combined preferences of a user is a dictionary in which, for application-specific domains, the keys are the bundle identifiers of applications.

image: Art/preference.jpg

Preferences are written to files named with the application’s bundle identifier and having an extension of plist. In iOS the files are located in a special location of an application’s sandbox; in Mac OS X preference files are located in Library/Preferences in a user’s home directory.

Your Application Must Present a User Interface for the Selection of Preferences

Before your application can store user-configurable preferences in the user defaults system, it must present a user interface for selecting those preferences. On Mac OS X, this entails designing a preference panel that a user displays by choosing Preferences from the application menu. The application is responsible for writing the settings that the user supplies to the user defaults system.

In iOS, the approach is more nuanced. Applications have two choices for displaying user preferences.

At runtime, your application retrieves preferences in the user defaults system using the NSUserDefaults or CFPreference APIs.

Facilities in Mac OS X for User Defaults

Mac OS X gives you two facilities to help you integrate an application’s user preferences with the user defaults system:

Definitive Discussion

    “Application Preferences”

Prerequisite Articles

Related Articles

Sample Code Projects

Did this document help you? Yes It's good, but... Not helpful...


Last updated: 2010-07-07