IPhone SDK: Application Preferences

An IPhone Application can be configured via the Settings menu, this tutorial illustrates how you can make a settings view for your app and then read the selected settings from your application.


Step 1: Create a Sample Application


I have used an application similar to the one created in the tutorial presented here (IPhone SDK Hello World).

Step 2: Create a Settings Bundle


Create a new File Command + N and then select Settings from the side menu and Settings  Bundle. The file must be named Settings.bundle to work correctly. 










This will create two new files Root.strings and Root.plist. Open the Root.plist file and you should see the following screen listing some sample settings. This is actually a graphical representation of an XML file which defines the settings and how they are displayed. (You can see the xml code by right clicking the file and selecting Open as Plain Text)




Step 3: Define Settings Schema


The Root element has three child elements (1) Title; which defines the title of this settings view and is set to the name of the application. (2) StringTable; which is not covered in this tutorial. (3) PreferenceSpecifiers which contains all the settings we wish to elicit from the user. PreferenceSpecifiers has type Array and has a child element of type Dictionary for each setting: Item 1, item2 and so forth. We can add items by selecting PreferenceSpecifiers and clicking the Grey button at the end of the row.

Each setting has several properties of type string including the common required properties; Title and Type (Note all strings are case sensitive). The title defines the label displayed beside the control used to modify the setting and type defines the type of control used. There are seven possible controls which can be selected using the Type property:


PSGroupSpecifier

This is the simplest control and has only the two properties defined described above. It is used as a separator to break-up groups of similar settings. It cannot accept any input from the user.


PSTitleValueSpecifier

This displays a "Read Only" setting it cannot accept input from the user but can be used to display settings changed elsewhere.


Key:

This is a String property which can be set to any text, the text is used as a key to map to the value that a user will enter into the control.  


DefaultValue:

Another String property which can store any text, this defines the Text that will be displayed in the specifier.



PSTextFieldSpecifier

The next type of control is the TextField, it has some additional properties which are listed below


Key:

This is a String property which can be set to any text, the text is used as a key to map to the value that a user will enter into the text field.  


DefaultValue:

Another String property which can store any text this defines the initial Text that will be entered into the TextField when it is first loaded.


IsSecure

This is a boolean property which defines if the charecters of the text field will be hidden, set to enabled for password fields.


KeyboardType

A String property which can have one of the following values: Alphabet, NumbersAndPunctuati on, NumberPad, URL, EmailAddress. This property specifies which type of keyboard to display when text field is selected


AutocapitalizationType

A String property which can have one of the following values: None, Sentences, Words, AllCharacters


AutoCorrectionType

A String property which can have one of the following values: Default, No, Yes
 
 

PSSliderSpecifier

This displays a slider on a bar which allows the user to select a number in a specified range. In addition to the standard Title and Type specifiers the Slider has the following properties.

Key:

This is a String property which can be set to any text, the text is used as a key to map to the value that a user will enter into the control.  

MinimumValue

A property storing a numerical value which corresponds to the slider being on the left end of the bar.

MaximumValue

A property storing a numerical value which corresponds to the slider being on the right end of the bar.

DefaultValue:

 A numerical  property which defines the original position of the slider. 



PSToggleSwitchSpecifier


Displays a toggle switch which can be either On or Off.

Key:

This is a String property which can be set to any text, the text is used as a key to map to the value that a user will enter into the control. 

TrueValue:

A boolean property which defines the value of the control of the toggle button is in the On position.

FalseValue:

A boolean property which defines the value of the control of the toggle button is in the Off position.

DefaultValue:

A boolean property which defines the position of the Toggle button the first time it is loaded.




PSMultiValueSpecifier

Multi-Value Specifier
Displays a list in a second view and allows the user to select one element from the list.

Key:

This is a String property which can be set to any text, the text is used as a key to map to the value that a user will enter into the control. 


Values:

A property of type Array which stores sub-elements of type String, each element provides a a pos sible value for the control to take. 


Titles:

A property of type array which stores sub-elements of type String, each element provides a textual representation of one of the values specified in the previous property. For example if the first element of the values array is "01" and the first element of the titles array is "January" then the user is shown January but if January is selected the value stored for the control is 01.  

DefaultValue:

Another String property that defines the initial value of the control, should be one of the elements of the Values list.

PSChildPaneSpecifier  

 

Key:

This is a String property which can be set to any text, the text is used as a key to map to the value that a user will enter into the control. 

File:

The name of another plist file without the extension.


Step 4: Retrieving Values of Settings

You can retrieve the value of a setting by using the command given below; Replace the hilighted text with the name of the setting you want to get the value of. 

NSString* settingValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"<Setting Key>"]



Step 5: Loading a Child Pane


The final of the Seven Controls allows you to add a sub view. The procedure to do so is to add a new file Command + N and select Other from the menu on the right Property List from the list of fle types.

Create a child prefernces schema exactly as you created the original schema.

No in the original schema add a PSChildPaneSpecifier and add a File item to the specifier with the file name of the child view (less the extension).

Technically this should be enough but as you will see if you run your code now the child view will not load. To load your child view the child plist file has to be inside the settings bundle but I can't find a way of doing this from within XCode (after half an our of random tinkering). So just open a terminal and move the file into the bundle manually. To do this browse to the directory containing your xcode project, and find child.plist file (or whatever you named it). Use mv child.plist Settings.bundle/child.plist to move your file and then click Build and Go in XCode.

If there is some way of moving files into the bundle through XCode that you know of please leave a coment and I will update this document.

 

Concluding Note


This document is a bit raw please leave feedback in case anything is not clear, you have some suggestions or if you spot any mistakes.

You can find the source code of the example at my website;
Source Code

Appendix A: NSLogs


To see the NSLog output generated by clicking the "Load Preferences" button in the sample code click the highlighted button in shown in the figure. It will open the log output window.

 

Comments

Untitled


how can I add pic in setting row item..as like as in pic.

how I add done button...pls help me

But here I can't add any picture.

add a small pic before PSTitleValueSpecifier.

It will be like that:

"pic + Title Value Read Only Value"

Pls help me as early as possible.

Last edited Mar 25, 2009 11:26 PM
Report abusive comment

Application preferences not showing up

Hello!

I'm developing official SDK applications but deploying them using Cydia on jailbroken phones. The preference bundle shows up on the simulator, but it doesn't in the device.

Any ideas why?

Last edited Feb 16, 2009 10:14 AM
Report abusive comment

Loading preferences doesn't work

When running your application I get the problem that the preferences won't be displayed when clicking the "Load Preferences" button. The action method is entered though, but the NSLogs aren't shown anywhere.

Am I making something wrong?

Last edited Nov 19, 2008 10:06 AM
Report abusive comment

Great Knol!

Good one, it was absolutely useful for me.
One thing though, you forgot to mention that the settings bundle must be named "Settings.bundle", otherwise, it won't show up in the preferences menu (at least that happened to me =)

Keep 'em up!

Last edited Sep 12, 2008 11:28 AM
Report abusive comment
Article rating:
Your rating:

Reviews

    Similar Content on the Web

    Knol translations

    Categories

    Based on community consensus.

    Activity for this knol

    This week:

    109pageviews

    Totals:

    21301pageviews
    10comments