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 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)
}
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")
Add first-launch detection to *already existing* iOS app
Based on your answer to the first few questions in the comments, the question you posted really should be rewritten to something like:
I currently have a free app. In the next update I wish to make some of the currently free functionality into paid functionality using in-app purchases. However, I don't wish for any existing users of the app to have to pay for the functionality they have been using already. Only new users of the app starting with this update should have to pay for the in-app purchase. How can implement my updated app so that downloaders of the previous version of the app do not have to pay?
Given this, your question has nothing to do with any sort of "first launch" detections. Your need is to see if the user "purchased" (or downloaded) the previous version of your app.
This can be accomplished by performing receipt validation. Once you obtain and verify the receipt, you can inspect the receipt and obtain an original purchase date and original purchase version. If that version is a version prior to adding the paid in-app purchase, treat the user as if they have made the in-app purchase already and show them the paid functionality of your app. Otherwise, check the receipt to see if it includes data on the in-app purchase and give user access as appropriate.
Detect first launch on multiple ViewControllers - Swift
Instead of checking isAppAlreadyLaunchedOnce check the launch of ViewController instead. So is firstVC is launch add to userDefaults firstVC as true. If second is launched set the secondVC as true. so this will maintain the userDefaults for individual VC's. Set your UserDefaults when OK was tapped. Then do this in all VC while you are checking the firstLaunch for each VC. Please keep the key different for different VC.
func isAppAlreadyLaunchedOnce()->Bool{
let defaults = NSUserDefaults.standardUserDefaults()
if defaults.boolForKey("firstVC") {
print("First VC launched")
return true
}else{
backgrdView.hidden = false
messageView.hidden = false
textView.hidden = false
okButton.hidden = false
tobysLabel.hidden = false
return false
}
}
@IBAction func okButtonTapped(sender: UIButton) {
backgrdView.hidden = true
messageView.hidden = true
tobysLabel.hidden = true
textView.hidden = true
okButton.hidden = true
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "firstVC")
}
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()
}
Related Topics
iOS Keeping Old Launch Screen and App Icon After Update
Connect Objective-C Framework to Swift iOS 8 App (Parse Framework)
Custom Installed Font Not Displayed Correctly in Uilabel
Use Multiple Font Colors in a Single Label
Objective C: Downloading File with Progress Bar
Restrict to Certain iOS Target Devices for App Store Submission
How to Convert My Device Token (Nsdata) into an Nsstring
Core Animation Progress Callback
Google Sign-In Crashes on iOS 9 Attempting to Call Canopenurl
Why Are Didbegincontact Called Multiple Times
Secitemadd and Secitemcopymatching Returns Error Code -34018 (Errsecmissingentitlement)
Swift 3.0 Error: Escaping Closures Can Only Capture Inout Parameters Explicitly by Value
Behaviour for Significant Change Location API When Terminated/Suspended
Logging Data on Device and Retrieving the Log
iPad Remembering Camera Permissions After Delete-How to Clear
How to Find File Uti for File, Withouth Pathextension, in a Path in Swift