I am trying to find a way in Swift to detect the first launch.
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
I did an edit of n13's post. This code seems cleaner to me. You can call as a class or instance function.
Also, according to apple docs you shouldn't call synchronize() since it's called periodically, unless the app is about to close. I have it called in the AppDelegate in applicationDidEnterBackground(). https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/#//apple_ref/occ/instm/NSUserDefaults/synchronize
I kinda always need this so I put it in a category
General Usage:
Usage inside your AppDelegate
Some important considerations:
Swift 4
Put the following in UserDefaults+isFirstLaunch.swift
Use NSUserDefaults. Register a BOOL key with a value of
false
. Read the key at launch time; if it'sfalse
, set it totrue
and show the welcome. Next launch, it will betrue
, you won't show the welcome, problem solved.you can use UserDefaults to store the times that App has opened
First:
AppDelegate.swift
each time the App is open, you should add the property currentTimesOfOpenApp, so modify this property in the function func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
in addition, when the app is closed, you should save the currentTimesOfOpenApp, that is important!
Second:
if you want to show the times, you can get this value form UserDefaults to display it on the Label.
ViewController.swift
the App is open every time, the currentTimesOfOpenApp will be increase. if you delete the App, this value will be reset as 1.
Swift 3
Swift 5 (Property wrappers)
UserDefaultWrapper:
UserDefaultsStore:
Usage:
I refined a bit user n13 answer in order to
Just use it wherever you want as
UIApplication.isFirstLaunch()
and be sure to reach it at least once during first execution.Swift 3
Swift 2