Count number of times app has been launched using Swift?

2 Solutions Collect From Internet About “Count number of times app has been launched using Swift?”

Add this in AppDelegate in applicationDidFinishLaunching method.

Swift 3 and Swift 4:

// get current number of times app has been launched
let currentCount = UserDefaults.standard.integer(forKey: "launchCount")

// increment received number by one
UserDefaults.standard.set(currentCount+1, forKey:"launchCount")

// save changes to disk
UserDefaults.standard.synchronize()

Swift 2:

// get current number of times app has been launched
let currentCount = NSUserDefaults.standardUserDefaults().integerForKey("launchCount")

// increment received number by one
NSUserDefaults.standardUserDefaults().setInteger(currentCount+1, forKey:"launchCount")

// save changes to disk 
NSUserDefaults.standardUserDefaults().synchronize()

You can store a int to NSUserDefaults.

Every time when you load the app, you can increase the number and save it again.

Add this logic in ApplicationDidFinishLaunching method.

Hope this helps.