How to detect first time app launch on an iPhone
Solution 1
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
return YES;
}
Solution 2
In Swift 3, 4 try this:
func isAppAlreadyLaunchedOnce()->Bool{
let defaults = UserDefaults.standard
if defaults.bool(forKey: "isAppAlreadyLaunchedOnce"){
print("App already launched : \(isAppAlreadyLaunchedOnce)")
return true
}else{
defaults.set(true, forKey: "isAppAlreadyLaunchedOnce")
print("App launched first time")
return false
}
}
In Swift 2 try this,
func isAppAlreadyLaunchedOnce()->Bool{
let defaults = NSUserDefaults.standardUserDefaults()
if defaults.boolForKey("isAppAlreadyLaunchedOnce"){
print("App already launched : \(isAppAlreadyLaunchedOnce)")
return true
}else{
defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce")
print("App launched first time")
return false
}
}
UPDATE:- For OBJ-C I use this,
+ (BOOL)isAppAlreadyLaunchedOnce {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isAppAlreadyLaunchedOnce"])
{
return true;
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isAppAlreadyLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
return false;
}
}
Ref for OBJ-C: https://stackoverflow.com/a/9964400/3411787
Solution 3
I wrote a tiny library for this very purpose. It lets me know whether this is the first launch ever, or just for this version, and any past versions the user has installed. It's available on github as a cocoapod under the Apache 2 license: GBVersionTracking
You just call this in application:didFinishLaunching:withOptions:
[GBVersionTracking track];
And then to check if this is the first launch just call this anywhere:
[GBVersionTracking isFirstLaunchEver];
And similarly:
[GBVersionTracking isFirstLaunchForVersion];
[GBVersionTracking currentVersion];
[GBVersionTracking previousVersion];
[GBVersionTracking versionHistory];
Solution 4
for Swift 3.0 - Swift 5
add extension
extension UIApplication {
class func isFirstLaunch() -> Bool {
if !UserDefaults.standard.bool(forKey: "hasBeenLaunchedBeforeFlag") {
UserDefaults.standard.set(true, forKey: "hasBeenLaunchedBeforeFlag")
UserDefaults.standard.synchronize()
return true
}
return false
}
}
then in your code
UIApplication.isFirstLaunch()
Solution 5
Another idea for Xcode 7 and Swift 2.0 is to use extensions
extension NSUserDefaults {
func isFirstLaunch() -> Bool {
if !NSUserDefaults.standardUserDefaults().boolForKey("HasAtLeastLaunchedOnce") {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "HasAtLeastLaunchedOnce")
NSUserDefaults.standardUserDefaults().synchronize()
return true
}
return false
}
}
Now you can write anywhere in your app
if NSUserDefaults.standardUserDefaults().isFirstLaunch() {
// do something on first launch
}
I personally prefer an extension of UIApplication like this:
extension UIApplication {
class func isFirstLaunch() -> Bool {
if !NSUserDefaults.standardUserDefaults().boolForKey("HasAtLeastLaunchedOnce") {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "HasAtLeastLaunchedOnce")
NSUserDefaults.standardUserDefaults().synchronize()
return true
}
return false
}
}
Because the function call is more descriptive:
if UIApplication.isFirstLaunch() {
// do something on first launch
}
Related videos on Youtube
Shishir.bobby
From Java to Android to iOS App Developer to Project Manager(now).
Updated on November 23, 2021Comments
-
Shishir.bobby over 2 years
How can I detect the very first time launch of
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // if very first launch than perform actionA // else perform actionB }
method?
-
Charan about 12 yearsI think stackoverflow.com/questions/5456134/iphone-launch-options will help you
-
-
Mati Bot about 11 yearskeep in mind that you need to lock this method if you want it to be thread safe.
-
Mati Bot about 11 yearsI know, but this method isn't. 2 Threads can reach the if(!flag){ when flag is NO. they will get inside the if blocks. one of them will get to the inner else and set the NSUserDefaults and the second one will pass the "hasLaunchOnce" (because the first thread declared it) and set the result to NO. That means you can get the wrong value.
-
Stian Høiland about 11 yearsThis is almost perfect! Would have been awesome without the GBToolbox dependency and a podspec.
-
tilo almost 11 yearsYour example works, but all the OP asks for is whether it is the first start or not. A bool is perfectly fine for this. Your code makes sense if one wants to know how many times the user opened the app.
-
mrEmpty over 10 yearsI get an error because the method isn't returning a boolean value. If I use return 0; to kill the error it fails.
-
Admin over 10 years@mrEmpty 1. ??? It returns
BOOL
. 2. Then the error is in your code... if returning 0 makes this crash, then something is terribly wrong -- elsewhere. -
Ziv Levy over 10 years@H2CO3 - isn't
NSUserDefaults
a common place? what if another application uses the same "key" that i'm using? -
Admin over 10 years@ZivLevy No, user defaults are stored in a property list on a per-sandbox (=per-application) basis.
-
lmirosevic over 10 years@StianHøiland GBToolbox dependency is gone, and library comes with a podspec (published to CocoaPods specs repo).
-
Aaron Ash about 8 yearsThere's an API for setting a default value for NSUserDefaults: developer.apple.com/library/ios/documentation/Cocoa/Reference/…:
-
Elad over 7 yearsIt will work only for new apps that never submitted before.
-
sigi over 6 yearsElad: You can version keys if you want... It works perfectly
-
VyacheslavBakinkskiy over 3 yearsIf you request for it in multiple places in the application, then it will return FALSE for the second and subsequent requests, although it may actually be the first run. Better to use a launch counter
-
V.March over 2 yearsHow does your answer differ from the answer https://stackoverflow.com/a/42509810/7802772 ?!