Re: NSUersDefaults and setting for a specific domain
Re: NSUersDefaults and setting for a specific domain
- Subject: Re: NSUersDefaults and setting for a specific domain
- From: Scott Morrison <email@hidden>
- Date: Tue, 26 Sep 2006 17:43:18 -0400
My experience is that [myDefaults
addSuiteName:@"com.mycompany.myapp"] works for reading defaults but
things get ugly when you want to save defaults.
While what Chris Park says works, it provides little in terms of
bindings.
So to extend the ability to use alternate domains, here is what I
have done for making use of bindings for preference panel controls.
In my plugin, I added a preference panel that uses bindings for
setting the prefs without needing a whole lot of glue code, but the
problem is that the SharedDefaultsController maps to the host
application default domain.
What I did was to prefix each plugin preference key with a unique prefix
Then on the save preferences method of the preference panel
controller, I move the preferences to the correct domain.
- (void) saveChanges {
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSDictionary * defaultDictionary = [defaults dictionaryRepresentation];
NSArray * keys = [defaultDictionary allKeys];
NSMutableDictionary * md = [[defaults
persistentDomainForName:@"com.mycompany.myapp"] mutableCopy];
if (!md) md=[NSMutableDictionary dictionary];
int keyIndex = [keys count];
while (keyIndex--){
NSString * key = [keys objectAtIndex:keyIndex];
if ([key hasPrefix:@"myPrefPrefix"]){
[md setObject:[defaultDictionary objectForKey:key] forKey:key];
[defaults removeObjectForKey:key];
}
}
[defaults setPersistentDomain:[md copy]
forName:@"com.mycompany.myapp"];
}
However in initialize from Defaults (in the preferences controller)
you also have to tell defaults to not apply immediately or else it
will not update the bound interface elements correctly. (it would not
seem to have the settings "stick")
- (void) initializeFromDefaults
{
[super initializeFromDefaults];
bool applyImmediateFlag = [defaultsController appliesImmediately];
[defaultsController setAppliesImmediately:NO];
// .... init from defaults code here
[defaultsController setAppliesImmediately:applyImmediateFlag];
}
and when your plugin loads you will have to addSuiteName:
________________________________
Scott Morrison <email@hidden>
Creator of Mail Act-On and Mail Tags plug-ins for OS X Mail.app.
<http://www.indev.ca/>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden