How do I detect when an iOS app is launched for the first time?
7 Answers
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:
Objective-C
// -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"];
Swift 5.0
// -applicationDidFinishLaunching:
UserDefaults.standard.register(defaults: ["firstLaunch":true])
// to check it:
UserDefaults.standard.bool(forKey: "firstLaunch")
// -applicationWillTerminate:
UserDefaults.standard.set(false, forKey: "firstLaunch")

- 1,610
- 14
- 28

- 57,021
- 16
- 130
- 131
-
1Are the entries in the defaults database that are associated with an application flushed when the application is uninstalled? – xyzzycoder Dec 29 '09 at 22:15
-
13With backgrounding now available, `-applicationWillTerminate:` is generally not called when the user quits the application. Instead `-applicationWillResignActive` is called. So firstLaunch should be set in both of those methods. – memmons May 05 '11 at 14:58
-
1Wouldn't this create a loop where every time the application is launched it sets firstLaunch to YES making every time a first launch? – TomLisankie Mar 04 '12 at 17:07
-
9Nope. `-registerDefaults:` only sets the values specified if their keys don’t already exist. – Noah Witherspoon Mar 05 '12 at 16:05
-
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];` – user213573 Dec 03 '09 at 06:16
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];

- 14,402
- 12
- 70
- 79
-
im always using this "check for first launch" code so this is a great addition! thanks for the work Clint! – nickthedude Jul 21 '10 at 22:07
-
1
-
2
-
but this doesn't work if the first version of the app is already in the app store, does it? – swalkner Aug 27 '12 at 06:28
-
-
It's quite old post but Very Useful. I wish apple would have given better approach detect these scenarios. – Kiran S Jul 17 '13 at 04:22
-
@zode64 if you drop this code into v2.0 of an existing app and post it to the app store, then the user upgrades to v2.0 from v1.0, then it will behave as if it is a new install. Reason being that the v1.0 did not have this code, so it will not have a 'prevStartupVersions' key. Workaround would be to put out a v1.1 that does nothing but install this key, and then put out v2.0 with the above code. – Scott Allen Sep 04 '13 at 18:04
-
@ScottAllen You're right and your solution is neat. When I was working with this problem I needed to respond to the first installation of the new version. Therefore it worked whether it was already installed or not and in that respect it doesn't matter if it is already installed. However there are possible scenarios where you might need to know both the version and if it is a new installation. – rogermushroom Sep 04 '13 at 18:38
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...
This is a really simple shortcut but I found that NSUserDefault key value pairs are always NULL the first time you run an app so
// Check to see if its the first time
if ([[NSUserDefaults standardUserDefaults] valueForKey:@"firstTime"] == NULL) {
[[NSUserDefaults standardUserDefaults] setValue:@"Not" forKey:@"firstTime"];
}
and place this code in the awakeFromNib of the view controller that appears when your application launches. I don't know if any of the other answers work for your problem, but this is the way I solved it.

- 1,594
- 5
- 25
- 47
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.

- 40,399
- 3
- 75
- 82
Save it as a user preference, eg had_first_launch, set to true on startup, it will only be false on the first time...

- 5,546
- 6
- 45
- 72
This will not work properly if you want to detect during other places of the code if its first launch. The "applicationWillTerminate" will not work from iOS 4.0 due to multitasking. this link provides a good solution: http://mobiledevblog.metalcompass.com/?p=43

- 21
- 1