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()
}
Detect when an iOS app is launched for the first time?
Pretty much what Marc and Chris said, though I prefer to change the value when the app quits in case there're multiple areas of the application that need to know about it.
In code:
Objective-C
// -applicationDidFinishLaunching:
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"firstLaunch",nil]];
// to check it:
[[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"];
// -applicationWillTerminate:
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
Swift 5.0
// -applicationDidFinishLaunching:
UserDefaults.standard.register(defaults: ["firstLaunch":true])
// to check it:
UserDefaults.standard.bool(forKey: "firstLaunch")
// -applicationWillTerminate:
UserDefaults.standard.set(false, forKey: "firstLaunch")
how to detect daily first launch in iOS?
Thank you guys! got an easy solution. Thanks for the support
let today = NSDate().formatted
if (NSUserDefaults.standardUserDefaults().stringForKey(Constants.FIRSTLAUNCH) == today)
{
//Already Launched today
}
else
{
//Today's First Launch
NSUserDefaults.standardUserDefaults().setValue(today, forKey:Constants.FIRSTLAUNCH)
}
extension NSDate {
var formatted: String {
let formatter = NSDateFormatter()
formatter.dateFormat = "MM/dd/yyyy"
return formatter.stringFromDate(self)
}
Related Topics
Swift: Nil Error When Using Self.Moc.Save() to Save in Core Data
Retrieve an Image from Firebase to an Uiimage Swift5
Get All Documents at Once in a Completion Handler with Getdocuments in Firestore
How to Deallocate All References Elements from an Array
Realitykit - How to Add Motion to a Loaded Modelentity from Usdz File
How to Create Lazy Combinations
Xcode Gm: No Swift Language for Os X Command Line Tool Project
Stanford Calculator App Keeps Crashing
Bulk Fix Hundreds of "#Selector Not Exposed to Objective-C" Errors in Xcode 9 or 9.1
How to Move a Model and Generate Its Collision Shape at the Same Time
Swift:Program for Addition of 2 Numbers Using Closure
Xcode 7.1 Beta: Content of File Error
Xcode Source Kit Will Not Stop Crashing with Swift
Contextual Member Has No Associated Value in Swift 3
How to Deal with Commas When Writing Objects to CSV in Swift
Generic Class with Class-Bound Constraint Cannot Be Parametrized by Class-Bound Protocol