PDA

View Full Version : Reset User Defaults?




tsornin
Jun 15, 2008, 02:11 PM
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?



Sayer
Jun 15, 2008, 06:09 PM
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];

UlfDunkel
Aug 24, 2010, 01:32 PM
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?

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

hotcocoa
Mar 20, 2011, 11:58 PM
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];
}


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

B U G E Y E
Apr 5, 2011, 07:44 PM
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];