Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Beginning iOS 5 Application Development
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 22nd, 2012, 09:06 AM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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?
Reply With Quote
  #2 (permalink)  
Old February 23rd, 2012, 06:55 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hello Brabbeldas,
From page 216 of the book:

Quote:
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:

Code:
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.

hth.
-Doug
__________________
===============================================
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."
===============================================
Reply With Quote
The Following User Says Thank You to dparsons For This Useful Post:
Brabbeldas (February 24th, 2012)
  #3 (permalink)  
Old February 24th, 2012, 03:49 AM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Hi Doug, thanks for replying.

On the internet I indeed found the same thing you say:
Quote:
“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

Code:
Code:
- (void)registerDefaultsFromSettingsBundle {
    // Find the Settings.bundle //
    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
    if(!settingsBundle) {
        NSLog(@"Could not find Settings.bundle");
        return;
    }
    // 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:
Code:
 // Initialize the settings value first //
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if(![defaults objectForKey:@"login_name"])
        [self registerDefaultsFromSettingsBundle];
I hope this helps someone.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:58 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.