Tuesday, February 3, 2009

iPhone Development: Application Preferences

Here I am going to discuss Application Preferences. For those who are unaware of it, Application preferences refers to the configurations that shows up in the Settings. So if you want you application name to be listed inside Settings, you need to follow the procedure.

Lets first discuss what are the available options. Since we know in iPhone all applications work in a sandbox mode, we can not write a program to alter the behavior of another program. What it means here is, there are fixed UI Elements that can be used in Application preferences, their behavior is also fixed and cannot be changed. For example a you cannot provide a date picker in the settings, you cannot pop up a message saying the URL you entered is not correct. All you can do is provide a field where user can enter his preferences.

Following is a list of options we have for UI elements that we can provide:

  1. PSTitleValueSpecifier: Used to write titles, similar to UILabel
  2. PSTextFieldSpecifier: Similar to UITextField
  3. PSToggleSwitchSpecifier: A toggle switch which can be ON or OFF
  4. PSSliderSpecifier: A slider which can be used to provide a value bw MAX and MIN
  5. PSMultiValueSpecifier: Shows multiple values from which user can select one.
  6. PSGroupSpecifier: Very important for layout (discussed later).
  7. PSChildPaneSpecifier: When you need to have another page of configurations.
All these elements can be used in the settings and you can read as well as manipulate them from your code.

Lets start the procedure. Open the same project "MyTestProject" we have been using last time. right click on the resources folder in left pane and select addNew. In the new window select Settings.bundle as shown in the figure.



Name it as Settings.bundle and click OK. This will create a Settings.bundle in your resources folder. Expand Settings.Bundle and double click on  Root.plist.

By default it should look like this: 

Right click on Preference Specifier and select "cut". I did this because in all my tutorials I do not rely on any convenience/ default things provided. It helps later in customizing things the way you want. Any ways now click on the  "+" button on the right side of the table and in place of "new Item" type PreferenceSpecifiers. In the type field corresponding to PreferenceSpecifiers, select "Array".

Now we will add one by one all the elements we listed earlier. Click on the Arrow symbol to the left of PreferenceSpecifiers to expand it. You will notice that the "+" image on right has now changed and it shows 3 lines. Click on it to add a child which will automatically be named as "Item 1". In the Type select "Dictionary". 
Now expand "Item 1" and add a child element to it, name it as "Title". Type should be "String" and in the Value write "URL".
Add more childs and make the plist file look like this:


Now Click on the arrow against "Item 1" to hide its child elements, click on "Item 1" (not on the arrow) and click on "+" button on right. This should show "Item 2" below "Item 1". Follow the last step correctly else you will end up wondering why I am not able to add a child or a new item at the place I want.

Add more items to make it look like this: 





Now build and run your project, go to Settings and find you applications' name there, clicking on it should show a screen like this:



I have only used three types in the preferences. In next post I will be using PSChildPaneSpecifier and add remaining of the elements i.e. PSTitleViewSpecifier, PSSliderSpecifier and PSGroupSpecifier.

Do not forget to mention name of your project "MyTestProject" against "Title", the second field from top in the pList file. 

An important thing to remember: "+" button in plist file adds a sibbling and the button with 3 lines adds a child.

If you faced any problem in creating child elements or new Items at the specific place in the plist file, don't worry, you just need to expand/hide elements properly. If you still face any problems, lemme know and I will be more than happy to help.

Topics of next post:
  1. Using PSChildPaneSpecifier and adding rest of the types.
  2. Accessing application preferences from inside your code
  3. Changing preferences from your code.
Also in next post we will be creating a custom TableView and custom Cells to display the application preferences in the form of a SetUp Wizard (all by writing code and not using Interface Builder).

 


2 comments:

  1. is it possible to add a picker in the settings menu, like the clock and date configuration ?

    ReplyDelete

Leave your suggestions

There was an error in this gadget