Objective-C NSUserDefaults and PreferenceSpecifiers on Root.plist

April 19, 2009 § Leave a comment

This is a very detail tutorial on how to start a System Setting Preferences and a setting bundle from scratch. It comes with Part 1 and Part 2. This is not a complete project that show you how to build the Interface Builder Preference table. However it gives bare bone with good explanation in creating a Preference setting bundle from new file, to switching the nil value to Default value.

To create a setting bundle for System Settings, you create a few file File > New File and select Settings in the side bar to create a Settings.bundle.

Settings.bundle contains Root.plist and Root.strings.

Root.string contains localized content. Root.plist defines the program’s system settings. Root.plist is an XML file and displayed as a list of keys and values within Xcode.

You must change the Title of the project name. There are four default preference types under PreferenceSpecifiers.

  • PSTextFieldSpecifier (UITextField)
  • PSTitleValueSpecifier (Current value of preference)
  • PSToggleSwtichSpecifier (UISwitch)
  • PSSliderSpecifier (UISlider)
  • PSMultiValueSpecificer (Points to a subpage containing a select list)
  • PSGroupSpecificer (Contains a group header for current table)
  • PSChildPaneSpecifier (Points to a subpage of preferences)

NSUserDefaults class reads the defaults that your application uses from a user’s defaults database at runtime. NSUserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value. Synchronize: method automatically invokes at periodic intervals, keeps the in-memory cache in sync with a user’s defaults database.

A default’s value must be a property list in NSdata, NSString, NSNUmber, NSDate, NSArry, or NSDictionary. Keep in mind that any values returned from NSUserDefaults are immutable. You can’t change the value from NSUserDefaults.

A defaults database is created automatically for each user.

This is how [NSUserDefaults standardUserDefaults] reads the data from PreferenceSpecifiers on Root.plist.

NSString *textValue = [[NSUserDefaults standardUserDefaults] stringForKey:@”textEntry_key”];
NSString *readOnlyValue = [[NSUserDefaults standardUserDefaults] stringForKey:@”readOnly_key”];
NSString *sliderValue = [[NSUserDefaults standardUserDefaults] stringForKey:@”slider_key”];
NSString *colorValue = [[NSUserDefaults standardUserDefaults] stringForKey:@”colors_key”];
NSString *toogleValue = [[NSUserDefaults standardUserDefaults] stringForKey:@”toogle_key”];

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Objective-C NSUserDefaults and PreferenceSpecifiers on Root.plist at Web Builders.

meta

%d bloggers like this: