How to detect first time app launch on an iPhone
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
return YES;
}
How to detect Apps first launch in iOS?
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.
How to check if the IOS app is running for the first time using SWIFT
Try this one
if(NSUserDefaults.standardUserDefaults().boolForKey("HasLaunchedOnce") {
// App already launched
} else {
// This is the first launch ever
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "HasLaunchedOnce")
NSUserDefaults.standardUserDefaults().synchronize()
}
Swift 3 version->
if (UserDefaults.standard.bool(forKey: "HasLaunchedOnce")) {
// App already launched
} else {
// This is the first launch ever
UserDefaults.standard.set(true, forKey: "HasLaunchedOnce")
UserDefaults.standard.synchronize()
}
How to detect the first launch of an app
Yo created function in function. Either move the isAlreadyLaunchedOnce
in the scope (to the same level as viewDidLoad
) and call it from viewDidLoad
or modify the code like this:
override func viewDidLoad() {
super.viewDidLoad()
println("Launched")
let defaults = NSUserDefaults.standardUserDefaults()
if let isAppAlreadyLaunchedOnce = defaults.stringForKey("isAppAlreadyLaunchedOnce"){
println("App already launched")
// do something
} else {
defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce")
println("App launched first time")
// do something
}
}
How to detect first time app launch on an iPhone
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
return YES;
}
Presenting an alert on first time app launch
You can use the UserDefaults
class to store simple keys. For example, you could store a boolean that tells you if this is the first launch :
func isFirstLaunch() -> Bool {
if (!UserDefaults.standard.bool(forKey: "launched_before")) {
UserDefaults.standard.set(true, forKey: "launched_before")
return true
}
return false
}
Then call this function and do the work you need in case it is the first launch :
if isFirstLaunch() {
// Do something
}
Related Topics
Uiview - How to Get Notified When the View Is Loaded
How to Create a New Swift Project Without Using Storyboards
What Is a "Delegate" in Objective C's iPhone Development
Uibezierpath Triangle with Rounded Edges
Custom Font Sizing in Xcode 6 Size Classes Not Working Properly with Custom Fonts
Programmatically Dial a Phone Number and Pass Dtmf Using the iPhone Sdk
Best Way to Parse Url String to Get Values for Keys
Uitableviewcell Buttons with Action
Refresh Devices in Team Provisioning Profile Managed by Xcode 7
iPhone Ad Hoc Build Using Xcode 4
Ios: Uibutton Resize According to Text Length
How to Change Text on a Back Button
iOS Autolayout with Uiscrollview: Why Does Content View of Scroll View Not Fill the Scroll View
How to Display a Uipopoverview as a Annotation to the Map View? (iPad)
Difference Between _ and Self. in Objective-C
How to Access Both Objective-C and Swift Classes from Same Storyboard
iOS Game Center: Scores Not Showing on Leaderboard in Sandbox