Re: Revert to factory settings: how to do it?
Re: Revert to factory settings: how to do it?
- Subject: Re: Revert to factory settings: how to do it?
- From: "R. Tyler Ballance" <email@hidden>
- Date: Mon, 11 Dec 2006 13:00:13 -0600
On Dec 11, 2006, at 12:47 PM, Arthur C. wrote:
I would like to have a set of preferences stored in the user
defaults. These preferences should have some initial values
("factory settings"), in case they have not been specified by the
user. According to the docs (User Defaults Programming Topics) this
can be done as follows:
+ (void) initialize
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary
dictionaryWithObjectsAndKeys: @"Arthur", @"firstName", @"C.",
@"lastName", nil];
[defaults registerDefaults:appDefaults];
}
The keys "firstName" and "lastName" have a binding to the shared
user defaults.
This works OK, but now I want to add a button like 'revert to
factory settings' to restore the initial values. I found that it
can be done as follows:
- (IBAction) revertToFactorySettings: (id) sender
{
[[ NSUserDefaults standardUserDefaults] removeObjectForKey:
@"firstName"];
[[ NSUserDefaults standardUserDefaults] removeObjectForKey:
@"lastName"];
}
But in this case I have to cycle through all the keys (what if
there are 100 of them?).
You can set it in a standard property list and set it from there
NSString *userDefaultsValuesPath;
NSDictionary *userDefaultsValuesDict;
// load the default values for the user defaults
userDefaultsValuesPath=[[NSBundle mainBundle]
pathForResource:@"UserDefaults"
ofType:@"plist"];
userDefaultsValuesDict=[NSDictionary
dictionaryWithContentsOfFile:userDefaultsValuesPath];
// set them in the standard user defaults
[[NSUserDefaults standardUserDefaults]
registerDefaults:userDefaultsValuesDict];
// Set the initial values in the shared user defaults controller
[[NSUserDefaultsController sharedUserDefaultsController]
setInitialValues:userDefaultsValuesDict];
That's how I do it anyways, unless I'm missing your question.
(Given, I don't have a factory defaults method to reset those values)
Cheers
R. Tyler Ballance: Lead Mac Developer at bleep. software
contact: email@hidden | jabber: email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden