Application Preferences


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.

Source: http://www.iphonesdkarticles.com/2008/08/application-preferences.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值