How to detect Apps first launch in iOS?
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
}
}
Swift 4 and higher
You can use this anywhere to verify that the user is seeing this view for the first time.
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 would return false
after user re-installs app and launch first time.
Since NSUserDefaults for an app are erased when uninstalling the app, you could try testing for the existence of a certain value when the app launches.
If the value exists, the app had already been installed. If not, this is the first time the app is launched, and you set that value.