Detect when an iOS app is launched for the first time?

Viewed 20280

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")

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...

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.

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

Related