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
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.
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];
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.
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
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.