ios 如何检测首次在 iPhone 上启动的应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9964371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:39:55  来源:igfitidea点击:

How to detect first time app launch on an iPhone

iosiphonelaunchlaunching-application

提问by Shishir.bobby

How can I detect the very first time launch of

我怎样才能检测到第一次启动

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // if very first launch than perform actionA
  // else perform actionB
}

method?

方法?

回答by

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    return YES;
}

回答by Zaid Pathan

In Swift 3, 4try this:

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
        }
    }

In Swift 2try this,

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
    }
}

UPDATE:-For OBJ-CI use this,

更新:-对于OBJ-C我使用这个,

+ (BOOL)isAppAlreadyLaunchedOnce {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isAppAlreadyLaunchedOnce"])
    {
        return true;
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isAppAlreadyLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return false;
    }
}

Ref for OBJ-C: https://stackoverflow.com/a/9964400/3411787

OBJ-C 参考:https: //stackoverflow.com/a/9964400/3411787

回答by lms

I wrote a tiny library for this very purpose. It lets me know whether this is the first launch ever, or just for this version, and any past versions the user has installed. It's available on github as a cocoapod under the Apache 2 license: GBVersionTracking

我为此目的编写了一个小库。它让我知道这是有史以来的第一次发布,还是仅针对此版本以及用户已安装的任何过去版本。它在 github 上作为 cocoapod 在 Apache 2 许可下可用:GBVersionTracking

You just call this in application:didFinishLaunching:withOptions:

你只要调用这个 application:didFinishLaunching:withOptions:

[GBVersionTracking track];

And then to check if this is the first launch just call this anywhere:

然后检查这是否是第一次发布,只需在任何地方调用它:

[GBVersionTracking isFirstLaunchEver];

And similarly:

同样:

[GBVersionTracking isFirstLaunchForVersion];

[GBVersionTracking currentVersion];
[GBVersionTracking previousVersion];
[GBVersionTracking versionHistory];

回答by KIO

for Swift 3.0 - Swift 5

适用于 Swift 3.0 - Swift 5

add extension

添加扩展

    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
        }
    }

then in your code

然后在你的代码中

UIApplication.isFirstLaunch()

回答by dennis-tra

Another idea for Xcode 7 and Swift 2.0 is to use extensions

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
    }
}

Now you can write anywhere in your app

现在你可以在你的应用程序的任何地方编写

if NSUserDefaults.standardUserDefaults().isFirstLaunch() {
    // do something on first launch
}

I personally prefer an extension of UIApplication like this:

我个人更喜欢这样的 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
    }
}

Because the function call is more descriptive:

因为函数调用更具描述性:

if UIApplication.isFirstLaunch() {
    // do something on first launch
}

回答by Mati Bot

You can implement it with the static method below:

您可以使用下面的静态方法来实现它:

+ (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;
}

回答by Phillip Mills

You need to save something when you launch and then check to see if it exists. If not, it's the first time. "Something" can be a file, a database entry, a setting in user defaults....

你需要在启动时保存一些东西,然后检查它是否存在。如果没有,那就是第一次。“某物”可以是一个文件、一个数据库条目、一个用户默认设置......

回答by Milo

It's quite simple to do this and requires only six lines of code.

这样做非常简单,只需要六行代码。

It will be useful to add this code in your application launch preferences or anywhere else you might need to test whether or not its the first time your application has been run.

将此代码添加到您的应用程序启动首选项或其他任何您可能需要测试它是否是您的应用程序第一次运行的地方会很有用。

//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. Do NOT use bools in preferencesJust stick to integers. They default to zero when undefined.

PS不要在首选项中使用 bool使用整数。未定义时,它们默认为零。

Also, the [theDefaults synchronize];line isn't required but I've found that when an app is ran hundreds of times across hundreds of devices, the results aren't always reliable, besides, it's better practice.

此外,该[theDefaults synchronize];行不是必需的,但我发现当一个应用程序在数百台设备上运行数百次时,结果并不总是可靠的,此外,这是更好的做法。

回答by Chris Fremgen

Quick and easy function

快速简便的功能

- (BOOL) isFirstTimeOpening {
    NSUserDefaults *theDefaults = [NSUserDefaults standardUserDefaults];
    if([theDefaults integerForKey:@"hasRun"] == 0) {
        [theDefaults setInteger:1 forKey:@"hasRun"];
        [theDefaults synchronize];
        return true;
    }
    return false;
}

回答by Malek_Jundi

store a bool key in NSUserDefaults first time it will be no you will change it to yes and keep it like that until the app delete or reinstall it will be again tha first time.

第一次在 NSUserDefaults 中存储一个 bool 键,它将不是,您将其更改为是并保持原样,直到应用程序删除或重新安装它将再次成为第一次。