如何检测首次启动
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// if very first launch than perform actionA
// else perform actionB
}
方法
答案 0 :(得分:377)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
return YES;
}
答案 1 :(得分:33)
在 Swift 3,4中试试这个:
func isAppAlreadyLaunchedOnce()->Bool{
let defaults = UserDefaults.standard
if let isAppAlreadyLaunchedOnce = defaults.string(forKey: "isAppAlreadyLaunchedOnce"){
print("App already launched : \(isAppAlreadyLaunchedOnce)")
return true
}else{
defaults.set(true, forKey: "isAppAlreadyLaunchedOnce")
print("App launched first time")
return false
}
}
在 Swift 2 试试这个,
func isAppAlreadyLaunchedOnce()->Bool{
let defaults = NSUserDefaults.standardUserDefaults()
if let isAppAlreadyLaunchedOnce = defaults.stringForKey("isAppAlreadyLaunchedOnce"){
print("App already launched : \(isAppAlreadyLaunchedOnce)")
return true
}else{
defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce")
print("App launched first time")
return false
}
}
更新: - 对于 OBJ-C ,我使用此功能,
+ (BOOL)isAppAlreadyLaunchedOnce {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isAppAlreadyLaunchedOnce"])
{
return true;
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isAppAlreadyLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
return false;
}
}
答案 2 :(得分:32)
您只需在application:didFinishLaunching:withOptions:
[GBVersionTracking track];
然后检查这是否是第一次启动只是在任何地方调用它:
[GBVersionTracking isFirstLaunchEver];
同样地:
[GBVersionTracking isFirstLaunchForVersion];
[GBVersionTracking currentVersion];
[GBVersionTracking previousVersion];
[GBVersionTracking versionHistory];
答案 3 :(得分:8)
您可以使用下面的静态方法实现它:
+ (BOOL)isFirstTime{
static BOOL flag=NO;
static BOOL result;
if(!flag){
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"hasLaunchedOnce"]){
result=NO;
}else{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
result=YES;
}
flag=YES;
}
return result;
}
答案 4 :(得分:8)
Xcode 7和Swift 2.0的另一个想法是使用扩展
extension NSUserDefaults {
func isFirstLaunch() -> Bool {
if !NSUserDefaults.standardUserDefaults().boolForKey("HasAtLeastLaunchedOnce") {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "HasAtLeastLaunchedOnce")
NSUserDefaults.standardUserDefaults().synchronize()
return true
}
return false
}
}
现在您可以在应用中的任何位置书写
if NSUserDefaults.standardUserDefaults().isFirstLaunch() {
// do something on first launch
}
我个人更喜欢像这样扩展UIApplication:
extension UIApplication {
class func isFirstLaunch() -> Bool {
if !NSUserDefaults.standardUserDefaults().boolForKey("HasAtLeastLaunchedOnce") {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "HasAtLeastLaunchedOnce")
NSUserDefaults.standardUserDefaults().synchronize()
return true
}
return false
}
}
因为函数调用更具描述性:
if UIApplication.isFirstLaunch() {
// do something on first launch
}
答案 5 :(得分:7)
for swift 3.0
添加扩展程序
extension UIApplication {
class func isFirstLaunch() -> Bool {
if UserDefaults.standard.bool(forKey: "hasBeenLaunchedBeforeFlag") {
UserDefaults.standard.set(true, forKey: "hasBeenLaunchedBeforeFlag")
UserDefaults.standard.synchronize()
return true
}
return false
}
}
然后在你的代码中
UIApplication.isFirstLaunch()
答案 6 :(得分:5)
您需要在启动时保存一些内容,然后检查它是否存在。如果没有,那是第一次。 "东西"可以是文件,数据库条目,用户默认设置....
答案 7 :(得分:4)
这样做很简单,只需要六行代码。
在您的应用程序启动首选项中添加此代码或在您的应用程序第一次运行时可能需要测试它的任何其他位置将非常有用。
//These next six lines of code are the only ones required! The rest is just running code when it's the first time.
//Declare an integer and a default.
NSUserDefaults *theDefaults;
int launchCount;
//Set up the properties for the integer and default.
theDefaults = [NSUserDefaults standardUserDefaults];
launchCount = [theDefaults integerForKey:@"hasRun"] + 1;
[theDefaults setInteger:launchCount forKey:@"hasRun"];
[theDefaults synchronize];
//Log the amount of times the application has been run
NSLog(@"This application has been run %d amount of times", launchCount);
//Test if application is the first time running
if(launchCount == 1) {
//Run your first launch code (Bring user to info/setup screen, etc.)
NSLog(@"This is the first time this application has been run";
}
//Test if it has been run before
if(launchCount >= 2) {
//Run new code if they have opened the app before (Bring user to home screen etc.
NSLog(@"This application has been run before);
}
P.S。 不要在首选项中使用bool 只需坚持整数即可。未定义时,它们默认为零。
此外,[theDefaults synchronize];
行不是必需的,但我发现当应用程序在数百台设备上运行数百次时,结果并不总是可靠的,此外,它是更好的做法。< / p>
答案 8 :(得分:3)
已更新 XCode 12 , Swift 5
extension UIApplication {
func isFirstLaunch() -> Bool {
if !UserDefaults.standard.bool(forKey: "HasLaunched") {
UserDefaults.standard.set(true, forKey: "HasLaunched")
UserDefaults.standard.synchronize()
return true
}
return false
}
然后您将其命名为
UIApplication.isFirstLaunch()
答案 9 :(得分:3)
对于Xcode 7中的Swift 2.0。 在AppDelegate.swift文件中:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool
{
return true
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
didFinishLaunchingOnce()
return true
}
func didFinishLaunchingOnce() -> Bool
{
let defaults = NSUserDefaults.standardUserDefaults()
if let hasBeenLauncherBefore = defaults.stringForKey("hasAppBeenLaunchedBefore")
{
//print(" N-th time app launched ")
return true
}
else
{
//print(" First time app launched ")
defaults.setBool(true, forKey: "hasAppBeenLaunchedBefore")
return false
}
}
}
答案 10 :(得分:3)
在NSUserDefaults中存储bool密钥第一次不会将其更改为是并保持这样,直到应用程序删除或重新安装它将是第一次。
答案 11 :(得分:2)
快速简便的功能
- (BOOL) isFirstTimeOpening {
NSUserDefaults *theDefaults = [NSUserDefaults standardUserDefaults];
if([theDefaults integerForKey:@"hasRun"] == 0) {
[theDefaults setInteger:1 forKey:@"hasRun"];
[theDefaults synchronize];
return true;
}
return false;
}
答案 12 :(得分:1)
我建议尽快使用一个全局常量,该常量可以在任何范围之外(例如在App委托上方)轻松完成。因此,只要不终止应用程序,它将设置为正确的值。如果应用程序进入后台运行状态,它将仍然返回相同的值。只有完全重新启动该应用程序,该值才会更改。
let isFirstLaunch: Bool = {
if !UserDefaults.standard.bool(forKey: "hasBeenLaunchedBeforeFlag") {
UserDefaults.standard.set(true, forKey: "hasBeenLaunchedBeforeFlag")
UserDefaults.standard.synchronize()
return true
}
return false
}()
但是说实话,最好跟踪该应用程序至少已发送到后台一次的事实。在这种情况下,我更喜欢在UIApplication上使用扩展,并在applicationDidEnterBackground方法中设置标志,以便:
extension UIApplication {
private static let isFirstLaunchKey = "isFirstLaunchKey"
static var isFirstLaunch: Bool {
return !UserDefaults.standard.bool(forKey: isFirstLaunchKey)
}
static func didEnterBackground() {
if isFirstLaunch {
UserDefaults.standard.set(true, forKey: isFirstLaunchKey)
UserDefaults.standard.synchronize()
}
}
}
,然后在您的应用程序委托或场景委托中
func sceneDidEnterBackground(_ scene: UIScene) {
UIApplication.didEnterBackground()
}
答案 13 :(得分:0)
最好的方法是使用NSUserDefaults
并保存BOOL
变量。
如上所述,以下代码可以正常运行:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:[NSNumber numberWithBool:true] forKey:@"~applicationHasLaunchedBefore"];
[userDefaults synchronize];
您还可以创建一个宏,如下所示,轻松检查是否是第一次启动
#define kApplicationHasLaunchedBefore [[NSUserDefaults standardUserDefaults] objectForKey:@"~applicationHasLaunchedBefore"]
然后使用它,
if (kApplicationHasLaunchedBefore) {
//App has previously launched
} else {
//App has not previously launched
}
答案 14 :(得分:0)
这是在Swift 5.0中工作的答案。与@Zaid Pathan的答案相比,改进之处在于没有隐藏的合同。如果您在调用setFirstAppLaunch()
之前没有完全一次调用isFirstAppLaunch()
,则会收到断言错误(仅在调试模式下)。
fileprivate struct _firstAppLaunchStaticData {
static var alreadyCalled = false
static var isFirstAppLaunch = true
static let appAlreadyLaunchedString = "__private__appAlreadyLaunchedOnce"
}
func setFirstAppLaunch() {
assert(_firstAppLaunchStaticData.alreadyCalled == false, "[Error] You called setFirstAppLaunch more than once")
_firstAppLaunchStaticData.alreadyCalled = true
let defaults = UserDefaults.standard
if defaults.string(forKey: _firstAppLaunchStaticData.appAlreadyLaunchedString) != nil {
_firstAppLaunchStaticData.isFirstAppLaunch = false
}
defaults.set(true, forKey: _firstAppLaunchStaticData.appAlreadyLaunchedString)
}
func isFirstAppLaunch() -> Bool {
assert(_firstAppLaunchStaticData.alreadyCalled == true, "[Error] Function setFirstAppLaunch wasn't called")
return _firstAppLaunchStaticData.isFirstAppLaunch
}
然后,您只需要在应用程序的开头调用函数setFirstAppLaunch()
,并在每次要检查是否已调用应用程序时调用isFirstAppLaunch()
。
答案 15 :(得分:0)
迅速
struct Pref {
static let keyFirstRun = "PrefFirstRun"
static var isFirstRun: Bool {
get {
return UserDefaults.standard.bool(forKey: keyFirstRun)
}
set {
UserDefaults.standard.set(newValue, forKey: keyFirstRun)
}
}
}
在应用启动时注册默认值:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let prefs: [String:Any] = [
Pref.keyFirstRun: true
...
]
UserDefaults.standard.register(defaults: prefs)
应用终止时的清除值:
func applicationWillTerminate(_ application: UIApplication) {
Pref.isFirstRun = false
检查值:
if Pref.isFirstRun {
... do whatever
答案 16 :(得分:0)
Swift 5 iOS 13。
Chris Fremgen让我轻松快捷。所以我更新了。
func isFirstTimeOpening() -> Bool {
let defaults = UserDefaults.standard
if(defaults.integer(forKey: "hasRun") == 0) {
defaults.set(1, forKey: "hasRun")
return true
}
return false
}