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.
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 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 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
Difference Between Uiviewcontentmodescaleaspectfit and Uiviewcontentmodescaletofill
How to Disable the Uitableview Selection
How to Compare Two Uiimage Objects
How to Upload Images to a Server in iOS With Swift
How to Hide a Status Bar in Ios
Why Is Wkwebview Not Opening Links With Target="_Blank"
How to Save an Array of Objects to Nsuserdefault with Swift
Display Uiviewcontroller as Popup in Iphone
Swift 3.1 Deprecates Initialize(). How to Achieve the Same Thing
Make Uinavigationbar Transparent
Always Pass Weak Reference of Self into Block in Arc
Xcode, Where to Assign the Segue Identifier
How to Animate the Background Color of a Uilabel
Xcode 6 - Xcassets For Universal Image Support
How to Build For Armv6 and Armv7 Architectures With iOS 5
Importing Project-Swift.H into a Objective-C Class...File Not Found