iPhone SDK Articles

Friday, August 15, 2008

Application Preferences


Welcome to the first part of Application Preferences tutorial

In this tutorial, I will show you how to create Application Preferences which will show up in Settings. This is how the Application Preferences will look like

Application preferences are stored in a plist file. So lets start by adding a settings bundle which will give us the plist file we need. To follow the steps below you need a project opened in XCode.

Select your project file in XCode (left side, in the file explorer) and Click File -> New File -> Click on Settings -> Select Settings Bundle and click Next. Give it a name and click on Finish.

Expand Settings.bundle (which is what my bundle is named) and you will find a file called Root.plist where all of our preferences will be stored.

Click on Root.plist -> Expand Preference Specifiers. It is under Preference Specifiers, that all of our preferences will go. Some of them are already created for us. For the sake of this tutorial, I will delete all of them and create new ones from scratch.

Every field that shows up in Preferences is created by creating a new Item. This Item always has a Type and a Title.

Possible values for Type are
  1. PSTextFieldSpecifier
  2. PSTitleValueSpecifier
  3. PSToggleSwitchSpecifier
  4. PSSliderSpecifier
  5. PSMultiValueSpecifier
  6. PSGroupSpecifier
  7. PSChildPaneSpecifier
Based on what type of specifier we set in the "Type" key , we have to supply its required keys.

PSTextFieldSpecifier
Select "PreferenceSpecifiers" and click on the icon that shows up at the end. As shown in the image below. A random item number is shown in the image because I have deleted all of the items.

Set the Type to Dictionary and we will be able to customize how this item/field shows up. When you set it to Dictionary, we will be able to add sub items to it, as seen in the image below. Expand the Item and we will be able to customize this item.

Set the Title to "Text Entry", Key to "textEntry" and Type to "PSTextFieldSpecifier". With the help of the key, we will be able to get the value in our application. We will also set some default value. Create a new item under Item1 and name the key "DefaultValue" and set the Type to String with some default value in the value field. This is how the first item will look like.

We can further customize this field, by adding what type of keypad shows up, correction type, if it is secure or not and auto capitalization type.

Key: IsSecure
Type: Boolean
Value: YES/NO

Key: KeyboardType
Type: String
Value: Value must contain one of the following strings. Alphabet, NumbersAndPunctuation, NumberPad, URL and EmailAddress.

Key: AutocapitalizationType
Type: String
Value: Value must contain one of the following strings. None, Sentences, Words, AllCharacters. Default value is None.

Key: AutoCorrectionType
Type: String
Value: Value must contain one of the following strings. Default, No, Yes. Default value is Default.

We would create different items in a similar way. Below is a list of all the keys, Types and values that an item can have.

PSTitleValueSpecifier

Key: Type (required)
Type: String
Value: PSTitleValueSpecifier

Key: Title (required)
Type: String
Value: Your string value.

Key: Key (required)
Type: String
Value: Your string value.

Key: DefaultValue (required)
Type: String
Value: Your string value.

Key: Values
Type: Array
Value: Key-Value entry

Key: Titles
Type: Array
Value: Key-Value entry

If Values key is used then it should have a corresponding value in the Titles array.

PSToggleSwitchSpecifier

Key: Type (required)
Type: String
Value: PSToogleSwitchSpecifier

Key: Title (required)
Type: String
Value: Your string value.

Key: Key (required)
Type: String
Value: Your string value.

Key: DefaultValue (required)
Type: String
Value: Your string value.

Key: TrueValue
Type: Boolean
Value: YES

Key: FalseValue
Type: Boolean
Value: NO

PSSliderSpecifier

Key: Type (required)
Type: String
Value: PSSliderSpecifier

Key: Title (required)
Type: String
Value: Your string value.

Key: Key (required)
Type: String
Value: Your string value.

Key: DefaultValue (required)
Type: String
Value: Your string value.

Key: MinimumValue (required)
Type: Number
Value: Minimum number value here

Key: MaximumValue (required)
Type: Number
Value: Maximum number value here

Key: MinimumValueImage
Type: String
Value: Image (21*21)

Key: MaximumValueImage
Type: String
Value: Image (21*21)

PSMultiValueSpecifier

Key: Type (required)
Type: String
Value: PSMultiValueSpecifier

Key: Title (required)
Type: String
Value: Your string value.

Key: Key (required)
Type: String
Value: Your string value.

Key: DefaultValue (required)
Type: String
Value: Your string value.

Key: Values
Type: Array
Value: Key-Value entry.

Key: Titles
Type: Array
Value: Key-Value entry.

PSGroupSpecifier

Key: Type (required)
Type: String
Value: PSGroupSpecifier

Key: Title (required)
Type: String
Value: Your string value.

PSChildPaneSpecifier

Key: Type (Required)
Type: String
Value: PSChildPaneSpecifier

Key: Title (required)
Type: String
Value: Your string value.

Key: File
Type: String
Value: The name of plist file, without the extension.

After we have defined our schema file, we can test it. Click on Build and Go and click the Home Button, Click on Settings and you will see the name of your application there. Click on it to see all the details. If there is an error in the item, that item may not show up or the application will crash.

We need some way to read that data and display it on the screen. We do this with the help of the following code

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"];


When the application is loaded for the first time, the view is going to have all null values. It looks like even though we set the default value, we have to write some code to set the default values when the application loads. More about this in Part 2 of this tutorial.

In this tutorial, I have created a new view and a view controller. It is in that view I display all the data from the preferences.

You can download the source code here.

Please leave me your comments and let me know know what are your thoughts. Your support is always appreciated.

9 comments:

Bob said...

Hi,

First of all, thanks for all these tutorials, they're a big help for me to get started with this stuff...

Secondly, isn't the toggle thing supposed to be named PSToggleSwitchSpecifier (with 2 g's instead of 2 o's)?

Thirdly, do I need to add the bundle somewhere to show up? I can't see them in settings, not even the default set I get when I make a new Settings bundle.

Any help is appreciated :)

Anonymous said...

I must be doing something wrong b/c I replicate the exact stuff in your tutorial and nothing comes up under the settings in the iPhone simulator. Your source code, however, works. Any ideas?

PlayBubi said...

I make the app and i transfer it on my iPod Touch, but i don't have my app preferences on Settings!! Why this?

jai said...

Hi Bob,

Thanks for pointing out that spelling mistake :). Late nights do not go very well with my spellings.

I added the settings bundle at the root so it is not contained in any folder.

Happy Programming,
iPhoneSDKArticles

jai said...

We have to set the defaults using code, please follow the second part of this tutorial here http://www.iphonesdkarticles.com/2008/08/application-preferences-part-2.html

Happy Programming,
iPhoneSDKArticles

xnakxx said...

you are dealing with just strings.( I am sure just for simplicity)
others might want to know that you can change the type of the value to number, string, Bool, Data, Date... in the root.plist. that way you don't have to deal with any conversions in your code if you are dealing with say an int value.

Leo said...

Hi there, thanks for all the info, it's all been very helpfull.

I am trying to duplicate the setup of the settings bundle in my application on the flipside view, but can't find an easy way to duplicate the multivalue field.
Do you have any suggestions?

Thank you kindly!

Leo K. said...

Hi there, thanks for all the info, it's all been very helpfull.

I am trying to duplicate the setup of the settings bundle in my application on the flipside view, but can't find an easy way to duplicate the multivalue field.
Do you have any suggestions?

Thank you kindly!

Leo K. said...

Hi there, thanks for all the info, it's all very helpfull.

I am trying to duplicate the layout of the settings bundle in my application on the flipside view, but can't find an easy way to duplicate the multivalue field.
Do you have any suggestions?

Thank you kindly!