Application Preferences - Parts2

Welcome to the Part 2 of Application Preferences tutorial. In this tutorial we will set the default values for this application. We do this in applicationDidFinishLoading method. If you haven't read the Part 1 of this tutorial, you can read it here. All the files references here are created in the first part of this tutorial.

Before we begin, open Root.plist file and set the Title to "RootPList". If you do not set a title then the following code may not work. That is what my experience has been.

FileName: ApplicationPreferencesTutorialAppDelegate.m
//It is here that we set the defaults
NSString *textValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"textEntry_Key"];

//If the first value is nil, then we know that the defaults are not set.
if(textValue == nil)
{
//Get the bundle path
NSString *bPath = [[NSBundle mainBundle] bundlePath];
NSString *settingsPath = [bPath stringByAppendingPathComponent:@"Settings.bundle"];
NSString *plistFile = [settingsPath stringByAppendingPathComponent:@"Root.plist"];

//Get the Preferences Array from the dictionary
NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFile];
NSArray *preferencesArray = [settingsDictionary objectForKey:@"PreferenceSpecifiers"];

//Temporary Variables
NSDictionary *item;
NSString *textEntry_Key;
NSString *readOnly_Key;
NSString *toogle_Key;
NSString *slider_Key;
NSString *colors_Key;

//Loop through the array
for(item in preferencesArray)
{
//Get the key of the item.
NSString *keyValue = [item objectForKey:@"Key"];

//Get the default value specified in the plist file.
id defaultValue = [item objectForKey:@"DefaultValue"];

if([keyValue isEqualToString:@"textEntry_key"])
textEntry_Key = defaultValue;

if([keyValue isEqualToString:@"readOnly_key"])
readOnly_Key = defaultValue;

if([keyValue isEqualToString:@"toogle_key"])
toogle_Key = defaultValue;

if([keyValue isEqualToString:@"slider_key"])
slider_Key = defaultValue;

if([keyValue isEqualToString:@"colors_key"])
colors_Key = defaultValue;
}

//Now that we have all the default values.
//We will create it here.
NSDictionary *appPrerfs = [NSDictionary dictionaryWithObjectsAndKeys:
textEntry_Key, @"textEntry_key",
readOnly_Key, @"readOnly_key",
toogle_Key, @"toogle_key",
slider_Key, @"slider_key",
[NSNumber numberWithInt:1], @"slider_key",
[NSNumber numberWithInt:1], @"colors_key",
nil];

//Register and save the dictionary to the disk
[[NSUserDefaults standardUserDefaults] registerDefaults:appPrerfs];
[[NSUserDefaults standardUserDefaults] synchronize];

We first have to check, if the defaults for this application has been set or not. If it is not then we have to create it, register it and sync it with the application.

In the source code above, I check if a value exists for a given key or not. If the value is nil then I know that the defaults have not been set.

We start by getting the path to our plist file and it is stored in the variable called plistFile.

//Get the bundle path
NSString *bPath = [[NSBundle mainBundle] bundlePath];
NSString *settingsPath = [bPath stringByAppendingPathComponent:@"Settings.bundle"];
NSString *plistFile = [settingsPath stringByAppendingPathComponent:@"Root.plist"];

We then create a dictionary with the contents of the property list file (plist file) and get the "PreferenceSpecifiers" into an array.

//Get the Preferences Array from the dictionary
NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFile];
NSArray *preferencesArray = [settingsDictionary objectForKey:@"PreferenceSpecifiers"];

Custom preferences were added into its own Array, so we will be able to loop through all the items in the array. We create temporary variables to hold the value and the key for every item we get from the array.

//Now that we have all the default values.
//We will create it here.
NSDictionary *appPrerfs = [NSDictionary dictionaryWithObjectsAndKeys:
textEntry_Key, @"textEntry_key",
readOnly_Key, @"readOnly_key",
toogle_Key, @"toogle_key",
slider_Key, @"slider_key",
[NSNumber numberWithInt:1], @"slider_key",
[NSNumber numberWithInt:1], @"colors_key",
nil];

//Register and save the dictionary to the disk
[[NSUserDefaults standardUserDefaults] registerDefaults:appPrerfs];
[[NSUserDefaults standardUserDefaults] synchronize];

Now that we have all the data we need, we will create another dictionary object with our key-value data. We then register the dictionary object using the registerDefaults method. registerDefaults will register the dictionary with a NSRegistrationDomain, which we do not need to create manually. It is simple a place where temporary variables can be set by the application. Then we call the synchronize method and it will write any modifications made to the domain to the disk.

If you have already downloaded this application, then you need to delete it from the simulator and run it again from XCode to see the default values is set.

This concludes the Part 2 of Application Preferences tutorial. You can download the source code here.


Source: http://www.iphonesdkarticles.com/2008/08/application-preferences-part-2.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值