Skip to content

Instantly share code, notes, and snippets.

@soffes
Created September 13, 2010 19:57
Show Gist options
  • Save soffes/577915 to your computer and use it in GitHub Desktop.
Save soffes/577915 to your computer and use it in GitHub Desktop.
Is there a better way?
// Original
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
static NSString *firstLaunchKey = @"firstLaunch";
// Register default defaults
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *defaultDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithBool:YES], firstLaunchKey,
nil];
[userDefaults registerDefaults:defaultDefaults];
[defaultDefaults release];
// First launch
if ([userDefaults firstLaunchKey] == YES) {
// Do first launch stuff here
[userDefaults setBool:NO firstLaunchKey];
}
// Finish initializing...
}
// One solution:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"hasLaunched"] == NO) {
// First launch, do stuff...
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasLaunched"];
}
}
@jakemarsh
Copy link

According to the docs, the contents of the registration domain are not written to disk; you need to call this method (registerDefaults:) each time your application starts.

You can place a plist file in the application's Resources directory and call registerDefaults: with the contents that you read in from that file.

[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"]]];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment