views:

1498

answers:

6

What's the quickest, easiest way to check if an application is being launched for the first time?

+2  A: 

You can set a boolean value in the user defaults to do this. Set the key to false when you call registerDefaults:, and then set it to true change it to true after you've shown your initial help screen or whatever you need to do.

If you have a persistent data file that's always saved after the app closes, checking to see if it exists would be another way.

Marc Charbonneau
+2  A: 

Save it as a user preference, eg had_first_launch, set to true on startup, it will only be false on the first time...

Chris Kimpton
+16  A: 

Pretty much what Marc and Chris said, though I prefer to change the value when the app quits in case there're multiple areas of the application that need to know about it. In code:

// -applicationDidFinishLaunching:
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"firstLaunch",nil]];
// to check it:
[[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"];
// -applicationWillTerminate:
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
Noah Witherspoon
Are the entries in the defaults database that are associated with an application flushed when the application is uninstalled?
xyzzycoder
@xyzzycoder: yes
Adam Woś
+14  A: 

I normally use the app version number instead of a boolean for the firstLaunch value in user defaults. That way, you can distinguish between the first launch of a new install and the first launch of an upgrade. May be useful in future versions...

+1  A: 

It is also good form to add a synchronize call to save it then and there. Not actually necessary in practice (at least on the current version of the OS). There is a sample titled AppPrefs in the documentation.

 [[NSUserDefaults standardUserDefaults] synchronize];
+5  A: 

I realize this question is quite old, but I used it to come up with one method of detecting the first startup after a "fresh install" (vs. first startup after an upgrade/downgrade) and thought I'd share the code here for future viewers in case it's helpful.

// Get current version ("Bundle Version") from the default Info.plist file
NSString *currentVersion = (NSString*)[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSArray *prevStartupVersions = [[NSUserDefaults standardUserDefaults] arrayForKey:@"prevStartupVersions"];
if (prevStartupVersions == nil) 
{
    // Starting up for first time with NO pre-existing installs (e.g., fresh 
    // install of some version)
    [self firstStartAfterFreshInstall];
    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:currentVersion] forKey:@"prevStartupVersions"];
}
else
{
    if (![prevStartupVersions containsObject:currentVersion]) 
    {
        // Starting up for first time with this version of the app. This
        // means a different version of the app was alread installed once 
        // and started.
        [self firstStartAfterUpgradeDowngrade];
        NSMutableArray *updatedPrevStartVersions = [NSMutableArray arrayWithArray:prevStartupVersions];
        [updatedPrevStartVersions addObject:currentVersion];
        [[NSUserDefaults standardUserDefaults] setObject:updatedPrevStartVersions forKey:@"prevStartupVersions"];
    }
}

// Save changes to disk
[[NSUserDefaults standardUserDefaults] synchronize];
Clint Harris
im always using this "check for first launch" code so this is a great addition! thanks for the work Clint!
nickthedude
I never thought of detecting upgrades and downgrades. +1.
JoePasq