Wrox Programmer Forums
BOOK: Beginning iOS 5 Application Development
This is the forum to discuss the Wrox book Beginning iOS 5 Application Development by Wei-Meng Lee; ISBN: 978-1-1181-4425-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning iOS 5 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 22nd, 2012, 09:06 AM
Registered User
Join Date: Feb 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Default Chapter9: application preferences question

When working my way through chapter 9 I run into a number of things I found a little funny.

First thing I noticed:
The book lets you start with adding the Settings.bundle file and lets you modify the Root.plist file. One of the things you do here is adding an item of type PSMultiValueSpecifier called 'Favorite Color'. Here you specify three colors (page 206/207).

Later you are asked to change the viewDidLoad method (page 211/212). Here you create and fill an array which will be used to fill the PickerView.
Should the PickerView not be filled with the items/colors we have just added to the Root.plist file?

Second thing I noticed(and this was a little weird to me):
At page 214 the book lets you change the application:didFinishLaunchingWithOptions: method. It lets you initialize the settings, which otherwise will be nil, and lets you set the default values..
Have we not already initialized the settings, and set their default values, inside the Root.plist file?
Old February 23rd, 2012, 06:55 PM
Wrox Author
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons

Hello Brabbeldas,
From page 216 of the book:

When your application runs for the first time, you need to set the values of the settings before you can use them. Hence, the best place to initialize them is in the application delegate.
This could of been explained a bit more but the reason that you created the array, set the values, and so on is because the values are nil the first time you run your application if the user hasn't run the settings app prior to the first run of the app itself.

That is why you do tests like this:

if (![defaults objectForKey:@”login_name”])
     [defaults setObject:@”login name” forKey:@”login_name”];
So if the value for login_name is nil we need to set the value.

Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
The Following User Says Thank You to dparsons For This Useful Post:
Brabbeldas (February 24th, 2012)
Old February 24th, 2012, 03:49 AM
Registered User
Join Date: Feb 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts

Hi Doug, thanks for replying.

On the internet I indeed found the same thing you say:
“the values are nil the first time you run your application if the user hasn't run the settings app prior to the first run of the app itself”
I find that a little strange but if that is the way it works.. oke.

If an app has a large number of Application Preferences its not that efficient to check and set them one by one. I have found a pice of code which does all that for you: It loads the default values from all the AppPreferences within the Root.plist

- (void)registerDefaultsFromSettingsBundle {
    // Find the Settings.bundle //
    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
    if(!settingsBundle) {
        NSLog(@"Could not find Settings.bundle");
    // Load the Root.plist into a Dictionary //
    NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
    // Load the PreferenceSpecifiers entry into an Array //
    NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
    // Create a new Dictionary which will contain all AppPreferences with their Defaut value //
    NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
    // Walk through all AppPrefrences loaded from the Root.plist //
    for(NSDictionary *prefSpecification in preferences) {
        // Get the 'Key' item from then specific AppPreference //
        NSString *key = [prefSpecification objectForKey:@"Key"];
        if(key) {
            // Add it to the defaultsToRegister Dictionary with its Default value //
            [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
    // Load the defaultsToRegister Dictionary into the UserDefaults class //
    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
Just call it once, only if the defaults have not been loaded yet:
 // Initialize the settings value first //
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if(![defaults objectForKey:@"login_name"])
        [self registerDefaultsFromSettingsBundle];
I hope this helps someone.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 9 -- Application Preferences FrankNatoli BOOK: Beginning iOS 4 Application Development 6 July 12th, 2011 02:44 PM
chapter9, 295. pas BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 5 October 1st, 2008 04:55 AM
Exception in chapter9 james.zeng BOOK: Beginning Cryptography with Java 2 January 11th, 2007 04:06 PM
Application Preferences And Resource Files geomar General .NET 0 November 14th, 2006 01:35 PM
XML, Sessions and Application Preferences pipelineconsulting Classic ASP XML 0 September 16th, 2005 04:41 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.