Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-02-2009, 04:21 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 18
Default Problems with NSUserDefaults

Beating my head against a wall on this one. For some reason I can no longer access my defaults file (settings bundle) from my app using NSUserDefaults (NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults] and then using keys to access individual settings). No error, I just get all zeroes for BOOL data types (using "boolForKey:") and NULL for NSStrings when I try to read them in. Is there something I might have changed in my Info.plist file or Project Settings that might broken the "link" to the Settings Bundle? Again, it WAS working and I did not change the code in my app, so I am thinking that it was perhaps a settings change. ANY help on this would be greatly appreciated! Thanks!

Correction: This behavior occurs only prior to directly accessing the settings for the app in the Settings application and making one change. After one change has been made, all the settings are subsequently loaded and saved by the app as they should be.

Last edited by BadgerDev; 03-02-2009 at 05:24 PM. Reason: More info
BadgerDev is offline   Reply With Quote
Old 03-02-2009, 06:31 PM   #2 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

The Settings bundle does not initially populate the user defaults. If you do a clean install of your app (after deleting the app from the sim or device) then the user defaults are empty. If you then run the Settings app and go to the section for your own app then the user defaults will get populated.

What you should do in your app is use 'registerDefaults' on the NSUserDefaults object. Use the same values as the default in your Settings bundle.

This way if a user runs your app without ever going to the Settings app, you will still get appropriate values from NSUserDefaults.
RickMaddy is offline   Reply With Quote
Old 03-02-2009, 09:12 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 18
Default

Quote:
Originally Posted by RickMaddy View Post
The Settings bundle does not initially populate the user defaults. If you do a clean install of your app (after deleting the app from the sim or device) then the user defaults are empty. If you then run the Settings app and go to the section for your own app then the user defaults will get populated.

What you should do in your app is use 'registerDefaults' on the NSUserDefaults object. Use the same values as the default in your Settings bundle.

This way if a user runs your app without ever going to the Settings app, you will still get appropriate values from NSUserDefaults.
Ahhh, thanks--got it. I had thought that the defaults were automatically loaded--my mistake.

I have produced the following code following your instruction (or trying to), but it still doesn't load the defaults. What am I missing here?

NSString *testValue = [myDefaults stringForKey:kFKey]; //check one non-nil value to see if defaults are already loaded
if (!testValue)
{
NSDictionary *initDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
NO, kPKey, @"Z", kFKey, YES, kMKey, NO, kCKey, YES, kVKey, nil];

[myDefaults registerDefaults:initDefaults];
[myDefaults synchronize];
}

Thanks again for your assistance.
BadgerDev is offline   Reply With Quote
Old 03-02-2009, 09:27 PM   #4 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

A few things. First off, just register the defaults every time. They won't interfere with the real values.

Second, you can put boolean values in a dictionary that way.

Last, no need to synchronize the defaults. They won't get written anyway. They just act as a backup in case a key isn't found.

So your code should be:

Code:
NSDictionary *initDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kPKey, @"Z", kFKey, [NSNumber numberWithBool:YES], kMKey, [NSNumber numberWithBool:NO], kCKey, [NSNumber numberWithBool:YES], kVKey, nil];

[myDefaults registerDefaults:initDefaults];
That's it. Just do this on app startup before you need to access anything from NSUserDefaults.
RickMaddy is offline   Reply With Quote
Old 03-02-2009, 09:50 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 18
Default

Quote:
Originally Posted by RickMaddy View Post
A few things. First off, just register the defaults every time. They won't interfere with the real values.

Second, you can put boolean values in a dictionary that way.

Last, no need to synchronize the defaults. They won't get written anyway. They just act as a backup in case a key isn't found.

So your code should be:

Code:
NSDictionary *initDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kPKey, @"Z", kFKey, [NSNumber numberWithBool:YES], kMKey, [NSNumber numberWithBool:NO], kCKey, [NSNumber numberWithBool:YES], kVKey, nil];

[myDefaults registerDefaults:initDefaults];
That's it. Just do this on app startup before you need to access anything from NSUserDefaults.
Beautiful! Worked like a charm--where do I send the royalty checks when my app hits the big time??? :-)

Thanks very much for your help--hopefully I can be of assistance to some folks here once I get the hang of this...have a good night.
BadgerDev is offline   Reply With Quote
Old 06-09-2009, 10:36 PM   #6 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 7
Default Problems with NSUserDefaults

wow, that's a horrible piece of infrastructure for you. what's the point of defining all your settings' default values in the plist file if you then have to also load them programmatically every time your app starts up?

that's awful. nice work, Apple.

good work on solving this, RickMaddy. too bad Apple doesn't even mention this in the API doc for NSUserDefaults.
n8r0n is offline   Reply With Quote
Old 11-19-2009, 04:49 PM   #7 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 1
Default RickMaddy Rocks!

First, Rick, thank you for clarifying why my iPhone app was having bi-polar issues. After reading your post, implementing the code, everything just started working, I then RTFD about NSUserDefaults and then Introduction to User Defaults, which I probably should have started-off with. Illuminating, but a bit frustrating to be honest. Still, as they say where I'm from, you gotta dance with the one who brung ya.

I just wanted to say thanks for pointing me, and I'm sure others, in the right direction. You saved my tail.

Quote:
Originally Posted by RickMaddy View Post
A few things. First off, just register the defaults every time. They won't interfere with the real values.

Second, you can put boolean values in a dictionary that way.

Last, no need to synchronize the defaults. They won't get written anyway. They just act as a backup in case a key isn't found.

So your code should be:

Code:
NSDictionary *initDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kPKey, @"Z", kFKey, [NSNumber numberWithBool:YES], kMKey, [NSNumber numberWithBool:NO], kCKey, [NSNumber numberWithBool:YES], kVKey, nil];

[myDefaults registerDefaults:initDefaults];
That's it. Just do this on app startup before you need to access anything from NSUserDefaults.
jdhouse4 is offline   Reply With Quote
Reply

Bookmarks

Tags
nsuserdefaults

Thread Tools
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 247
13 members and 234 guests
2WeeksToGo, AdamL, ADY, BrianSlick, Dani77, Dattee, headkaze, mer10, mgon987, timle8n1, Touchmint, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,879
Threads: 89,228
Posts: 380,745
Top Poster: BrianSlick (7,129)
Welcome to our newest member, mgon987
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:46 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0