WHAT YOU WILL LEARN IN THIS CHAPTER
If you are a relatively seasoned Mac OS X user, you're likely familiar with the concept of application preferences. Almost every Mac OS X application has application-specific settings that are used for configuring the application's appearance and behavior. These settings are known as the application preferences.
In the iPhone OS, applications also have application preferences. In contrast to Mac OS X applications, however, whose application preferences are an integral part of the application, iPhone preferences are centrally managed by an application called Settings (see Figure 11-1).
The main page of the Settings application displays the preferences of system applications as well as third-party applications. Tapping any setting brings you to another page, where you can configure the preferences of an application.
In this chapter, you learn how to incorporate application preferences into your application and modify them programmatically during runtime.
Creating application preferences for your iPhone 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 iPhone application project in Xcode.
The preferences settings are of little use if you can't programmatically access them from within your application. In the following sections, you modify the application so that you can load the preferences settings as well as make changes to them programmatically.
First, use the following Try It Out to prepare the UI by connecting the necessary outlets and actions.
With the user interface of the application ready, it is now time to see how you can programmatically load the values of the preference settings and then display them in your application. This display is useful because it gives your user a chance to view the values of the settings without needing to go to the Settings application.
Sometimes you may want to reset the values of the preference settings of your application. This is especially true if you have made an error in the Root.plist
file and want to reset all the settings. The easiest way to do this is to remove the application from the phone or Simulator. To do so, simply tap and hold the application's icon, and when the icons start to wriggle, tap the X button to remove the application. The preference settings associated with the application will also be removed.
Another way to clear the values of the preference settings would be to navigate to the folder containing your application (on the iPhone Simulator). The applications on the iPhone Simulator are stored in the following folder: ~/Library/Application Support/iPhone Simulator/User/Applications/
(note that the tilde symbol (~
) represents your home directory and not your root hard disk). Inside this folder, you need to find the folder containing your application. Within the application folder is a Library/Preferences
folder. Delete the file ending with application_name.
plist
(see Figure 11-19) and your preferences settings will now be reset.
Now that you have seen how to load the values of preferences settings, use the following Try It Out to see how to save the values back to the preferences settings. This allows users to directly modify their preferences settings from within your application, instead of using the Settings application to do so.
In this chapter, you have seen how you can make use of the Application Preferences feature of the iPhone to save your application's preferences to the Settings application. Doing so allows you to delegate most of the mundane tasks of saving and loading an application's preferences settings to the OS. All you need to do is to use the NSUserDefaults
class to programmatically access the preferences settings.
EXERCISES
You have learned that you can use the NSUserDefaults
class to access the preferences settings values for your application. What are the methods for retrieving and saving the values?
What are the two ways in which you can remove the preferences settings for an application?
What is the difference between the Add Child button and the Add Sibling button in the Property List editor?
WHAT YOU'VE LEARNED IN THIS CHAPTER
TOPIC | KEY CONCEPTS |
---|---|
Adding application preferences to your application | Add a Settings Bundle file to your project and modify the |
Loading the value of a preference setting |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *strLoginname = [defaults objectForKey:@"login_name"]; |
Resetting preference settings values | Either remove the entire application from the Home screen, or remove it via the iPhone Simulator folder on your Mac. |
Saving the value of a preference setting |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:loginName.text forKey:@"login_name"]; |