Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

default values for NSUserDefaults?

trappertrapper Posts: 299Registered Users
edited June 2011 in iOS SDK Development
I’ve got NSUserDefaults working fine in my app now, but upon doing a clean test I noticed that my app is missing default values for these ‘defaults’ so they remain as blanks until the user sets them for the first time.

How can I put some actual default values in there for this first use of my app?
Post edited by trapper on
<a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=294154344&mt=8" target="_blank">Black or Red</a> - Possibly the first ever iPhone drinking game! Try it tonight.<br />
<a href="http://itunes.apple.com/WebObjects/MZStore.woa/
· ·

Replies

  • StitchStitch Posts: 401Registered Users @ @
    edited August 2008
    In application did finish loading just check the value in the ones you are worried about and if null then set them to a default.

    This is what I do and it works fine. The first time they install it sets about 6 or 7. I also run syncronize just after to ensure they are saved and updated.
    <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=311941449&mt=8" target="_blank">BUZZER!</a> : <font color="Sienna">iTunes Library Music Quiz (1 or 2 Player)</font>
    · ·
  • trappertrapper Posts: 299Registered Users
    edited August 2008
    Yeah I suppose that is the easiest way aye. Will do it like that.
    <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=294154344&mt=8" target="_blank">Black or Red</a> - Possibly the first ever iPhone drinking game! Try it tonight.<br />
    <a href="http://itunes.apple.com/WebObjects/MZStore.woa/
    · ·
  • scottiphonescottiphone Posts: 802Registered Users
    edited August 2008
    You should up the defaults when your app loads. Then if there is a default file already setup fro the last time it will use those values. Otherwise it will use your preset defaults.
    save defaults when terminating so if the user has changed anything within your app it's saved.

    Page 204 in iPhone Programming Guide.
    · ·
  • trappertrapper Posts: 299Registered Users
    edited August 2008
    Yeah I got the setting and getting of NSUserDefaults all working fine. The problem was starting off a new install with some pre-defined values. Do you mean I can do that using a plist in the setting.bundle? I thought this was for creating a new tab in the settings app.
    <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=294154344&mt=8" target="_blank">Black or Red</a> - Possibly the first ever iPhone drinking game! Try it tonight.<br />
    <a href="http://itunes.apple.com/WebObjects/MZStore.woa/
    · ·
  • scottiphonescottiphone Posts: 802Registered Users
    edited August 2008
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSNumber *testValue = [NSNumber numberWithFloat:1.0];
    NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:
    testValue, @"MyTestValue" , ...

    [defaults registerDefaults:appDefaults];
    [appDefaults release];
    · ·
  • skinrockskinrock Posts: 177Registered Users
    edited August 2008
    That still doesn't really show how you know if you should set default values in the first place. I'm actually running into this now, because I want to implement user settings without getting into sqlite (I know how to use sqlite, but this is already available).

    The "check if null" would be great, but I most of my settings are integers, and you can't compare an integer to NULL.

    edit: sorry, I read the class reference wrong, it seemed like this would always set the default values. This actually seems to work great, in that it doesn't overwrite the values once you change them.
    · ·
  • scottiphonescottiphone Posts: 802Registered Users
    edited August 2008
    There's no SQL involved. This is designed for defaults. You set them in your init. Then you load in any that you saved the last time. If none then your defaults are used. If there is a default file it's loaded and you use that. Save defaults when terminating.
    · ·
  • donutzeatpeopledonutzeatpeople Posts: 2New Users
    edited June 2011
    I came upon this thread before finding another, more automatic way to initially setup the user defaults (that is, without relying on the Settings.app being opened by the user):

    NSUserDefaults nil Setting Problem - The B-Log

    In a nut, this routine gets put in your applicationDidFinishLaunchingWithOptions: method and as soon as you app launches, it takes the DefaultValues from your settings bundle and makes them the selected value for each of your user defaults.

    Hope this helps someone in the future.
    · ·
  • Duncan CDuncan C Posts: 9,082Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited June 2011
    I came upon this thread before finding another, more automatic way to initially setup the user defaults (that is, without relying on the Settings.app being opened by the user):

    NSUserDefaults nil Setting Problem - The B-Log

    In a nut, this routine gets put in your applicationDidFinishLaunchingWithOptions: method and as soon as you app launches, it takes the DefaultValues from your settings bundle and makes them the selected value for each of your user defaults.

    Hope this helps someone in the future.

    No, the way in your link is not the easiest/cleanest way.

    There is a system call "registerDefaults" That lets you provide a dictionary of starting default values. The system automatically first looks for a user-specified setting. That overrides a starting value. Only if your app has not saved a new value, it then looks in the dictionary of starting values you provide in your call to registerDefaults.

    The registerDefaults call is specifically for this situation.

    Take a look at scottiphone's post, from earlier in this thread:
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    	NSNumber *testValue = [NSNumber numberWithFloat:1.0];
    	NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:
    								 testValue, @"MyTestValue"; , ...
    
    [defaults registerDefaults:appDefaults]; 
    	[appDefaults release];
    
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
    · ·
  • donutzeatpeopledonutzeatpeople Posts: 2New Users
    edited June 2011
    Ahh... thanks for clarifying that. I didn't understand what was going on in scottiphone's code the first time.
    · ·
Sign In or Register to comment.