Reset User Defaults?

Discussion in 'Mac Programming' started by tsornin, Jun 15, 2008.

  1. tsornin macrumors newbie

    Jul 23, 2002
    I'm working through the new Cocoa Programming book and one of the exercises is to add a button for the user to reset their preferences (drop any changes made to their NSUserDefaults and revert to the factory defaults).

    In looking at the docco for NSUserDefaults, I see there's a class method "resetStandardUserDefaults" which sounds like it might do what I want... until I read the description: "Synchronizes any changes made to the shared user defaults object and releases it from memory." I'm not sure what the point of that is, but it doesn't seem like it does what I want. I tried it in a bit of code, but nothing actually changed:

    	[NSUserDefaults resetStandardUserDefaults];
    	[colorWell setColor:[self tableBgColor]];
    	[checkbox setState:[self emptyDoc]];
    I'm guessing that just saves the changed prefs out to disk, and then the setColor method reads in the (changed) stuff again. So nothing was actually reset.

    Right now I'm thinking the only way to reset the prefs will be to loop through all of the keys in the standard user defaults object and set each one back to whatever the initial value was. Is that the correct way, or am I misunderstanding what resetStandardUserDefaults does?
  2. Sayer macrumors 6502a


    Jan 4, 2002
    Austin, TX
    You can create a plist file with "default" values and set that as the standard defaults for use when the pref isn't set yet (new install) or you want to revert to "factory" settings.

    Without having the book you are working through I can't say anything one way or another since I don't know what the sample project has in it.

    I used this for pre-made default preferences:

        NSString *userDefaultsValuesPath;
        NSDictionary *userDefaultsValuesDict;
        // load the default values for the user defaults
        userDefaultsValuesPath=[[NSBundle mainBundle] pathForResource:@"MacAdShowDefaults" ofType:@"plist"];
        userDefaultsValuesDict=[NSDictionary dictionaryWithContentsOfFile:userDefaultsValuesPath];
        // set them in the standard user defaults
        [[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsValuesDict];
  3. UlfDunkel macrumors newbie

    Aug 24, 2010
    This seems to be the best way, as [NSUserDefaults resetStandardUserDefaults]; doesn't do what you want to do.

    Here is my solution for the same Challenge from "Cocoa Programming in Mac OS X" (Hillegass):

    - (IBAction)resetPreferences:(id)sender
       NSColor *defaultColor = [NSColor yellowColor];
       NSData  *colorAsData  = [NSArchiver archivedDataWithRootObject:defaultColor];
       // reset defaults in the dictionary
       [[NSUserDefaults standardUserDefaults] setObject:colorAsData forKey:DSDTableBackgroundColorKey];
       [[NSUserDefaults standardUserDefaults] setBool:YES           forKey:DSDCreateEmptyDocumentKey];
       // update UI: reset default color
       [colorWell setColor:defaultColor];
       // update UI: reset default checkbox
       [checkbox setState:YES];
  4. hotcocoa macrumors member

    Jan 30, 2011
    Rather than duplicating the logic that defines the values of the defaults, you can simply remove the non-default values stored in [NSUserDefaults standardUserDefaults] using the -(void)removeObjectForKey method.

    Here is my solution:
    - (IBAction)resetAllPreferences:(id)sender
    	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    	// clear the non-default values
    	[userDefaults removeObjectForKey:BNRTableBgColorKey];
    	[userDefaults removeObjectForKey:BNREmptyDocKey];
    	// update the preference window to reflect the new defaults
    	[self windowDidLoad];
  5. B U G E Y E macrumors newbie

    Apr 2, 2009
    Portland, OR
    If you want to blow away all your defaults, this snippet has worked for me:

    NSDictionary *defaultsDict = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
    for (NSString *key in [defaultsDict allKeys])
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];

Share This Page