Skip to content

Instantly share code, notes, and snippets.

@ejknapp
Created December 1, 2009 02:13
Show Gist options
  • Save ejknapp/246002 to your computer and use it in GitHub Desktop.
Save ejknapp/246002 to your computer and use it in GitHub Desktop.
//NSUserDefaults usage for user and app settings
- (void)createUserDefaultsFromPlist {
NSString *settingsPlistPath = [[NSBundle mainBundle]
pathForResource:@"FlakSettings" ofType:@"plist"];
NSDictionary *dictionaryFromDefaultPlist;
if (settingsPlistPath) {
dictionaryFromDefaultPlist = [NSMutableDictionary
dictionaryWithContentsOfFile:settingsPlistPath];
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[dictionaryFromDefaultPlist objectForKey:@"currentHostURL"]
forKey:@"currentHostURL"];
[defaults setObject:[dictionaryFromDefaultPlist objectForKey:@"savedHosts"]
forKey:@"savedHosts"];
[defaults setObject:[dictionaryFromDefaultPlist objectForKey:@"loginData"]
forKey:@"loginData"];
}
- (id)init {
if (self = [super init]) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *currentHostURL = [userDefaults stringForKey:@"currentHostURL"];
//Load bundle plist if settings file doesn't exist
if (currentHostURL == nil) {
NSLog(@"User defaults not found so setting them from plist");
[self createUserDefaultsFromPlist];
} else {
NSLog(@"User defaults set so nothing to do");
}
}
return self;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment