How to detect if application run for the first time in iOS using Swift 3

How to detect if app is running for the first time

You can detect if the application is launched for the first time using NSUserDefaults.

Just by looking for a key called “launchedBefore” for example and if its not found then the application is running for the first time.

if not found then create it.

Add the following code to didFinishLaunchingWithOptions() in AppDelegate.swift file

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

You might like How to add underline to UITextField in Swift 3

Leave a Reply

Your email address will not be published. Required fields are marked *