Chapter 15. Defaults and Preferences

Most applications need to store and retrieve preferences that allow for user customization of an application’s behavior and keep track of configuration settings. Mac OS X provides, as part of its Core Foundation framework, a preferences system that provides a simple and standard way to maintain these preferences. Cocoa calls these preferences defaults.

If you go to the ~/Library/Preferences folder, you will see the user-preferences database used by the applications you run on your system. In fact, many of the applications we have created in this book have written preferences to this database. Take a look, and you’ll see that you probably have Dot View.plist, Menu.plist, and RTF Edit.plist files. These contain preferences used by some of the various Cocoa base classes used in the sample applications we’ve built throughout the book.

In this chapter we’ll show you how to take advantage of the user-preferences system from your applications using Cocoa.

How Preferences Work

The preference system in Mac OS X allows you to store values associated with a key—the name of a property—that can later be used to look up the preference value when you need it. These key-value pairs are scoped using a combination of username and application ID. All of the preferences for a user are stored in his ~/Library /Preferences folder.

There are multiple domains—or different scopes of coverage—in which a preference can exist. When you request a preference, the following ...

Get Learning Cocoa with Objective-C, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.