First launch of iOS app

I want to show the welcome screen when the user first opens my application. What is the way to test the first application launch in Swift?

+9
source share
4 answers

Swift 4 and above

func isAppAlreadyLaunchedOnce()->Bool{
    let defaults = UserDefaults.standard
    if let _ = defaults.string(forKey: "isAppAlreadyLaunchedOnce"){
        print("App already launched")
        return true
    }else{
        defaults.set(true, forKey: "isAppAlreadyLaunchedOnce")
        print("App launched first time")
        return false
    }
}

Note. This method returns falseafter the user reinstalls the application and launches it for the first time.

+18
source

Try this for Swift 2 and below.

func isAppAlreadyLaunchedOnce()->Bool{
    let defaults = NSUserDefaults.standardUserDefaults()

    if let isAppAlreadyLaunchedOnce = defaults.stringForKey("isAppAlreadyLaunchedOnce"){
        println("App already launched")
        return true
    }else{
        defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce")
        println("App launched first time")
        return false
    }
}
+14
source

Since NSUserDefaults for the application are erased when the application is uninstalled, you can try to test for a certain value when the application starts.

If this value exists, the application is already installed. If not, this is the first time the application starts, and you set this value.

+9
source

SWIFT:

let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
if launchedBefore  {
    print("Not first launch.")
} else {
    print("First launch, setting UserDefault.")
    UserDefaults.standard.set(true, forKey: "launchedBefore")
}

GOAL - WITH:

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isAppAlreadyLaunchedOnce"])
{
    return true;
}
else
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isAppAlreadyLaunchedOnce"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    return false;
}
+1
source

All Articles