2

Possible Duplicate:
Is there anyway to set values in the settings.bundle from within the APP

I'm developing an iPhone app that will at some point prompt a user alerting them that a setting is switched off. From there they can choose to enable it and continue. I'm able to get the settings and use then in my code, the problem is changing them programmatically. Here's what I have

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex == 1){

        // Get the application defaults
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"YES" forKey:@"enabledAutoCheck"];

        // Set the new settings and save
        [defaults registerDefaults:appDefaults];
        [defaults synchronize]; 
     }
}

I'm making a wild guess (since I only started learning Objective-C and iPhone development 3 days ago!) that it has something to do with the fact that this is setting the default settings meaning they are only used if no previous settings are made.

Is this just missing a little something or is there another way to change settings once they've been set initially?

Thanks

Community
  • 1
  • 1
Peter
  • 549
  • 7
  • 20

1 Answers1

4

NSUserDefaults is a bit of a misleading name - they are the actual settings, not the defaults.

You don't need registerDefaults - instead you could call [defaults setObject:@"YES" forKey:@"enabledAutoCheck"]; (store a setting) then [defaults synchronize]; (save the settings). That way you avoid erasing settings you don't want to erase.

The rest of your code is fine. However, do make sure to actually load them where you need them, and make sure that you change the affected UI parts when you change a UI setting. They won't update themselves, you know :-)

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
  • Thank you so much! Works perfectly :) Yeah I know, I'm using the InAppSettings kit which take's care of the UI side of things. – Peter Dec 03 '11 at 21:18