How to detect first time app launch on an iPhone

2018-12-31 23:46发布

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?

标签: iphone ios
12条回答
妖精总统
2楼-- · 2019-01-01 00:00

NSUserDefaults + Macro

The best approach is to use NSUserDefaults and save a BOOL variable. As mentioned above, the following code will do just fine:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:[NSNumber numberWithBool:true] forKey:@"~applicationHasLaunchedBefore"];
[userDefaults synchronize];

You can also create a macro as below to easily check whether it is the first launch or not

#define kApplicationHasLaunchedBefore [[NSUserDefaults standardUserDefaults] objectForKey:@"~applicationHasLaunchedBefore"]

Then use it as such,

if (kApplicationHasLaunchedBefore) {
    //App has previously launched
} else {
    //App has not previously launched
}
查看更多
只若初见
3楼-- · 2019-01-01 00:01

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

You just call this in 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];
查看更多
唯独是你
4楼-- · 2019-01-01 00:01

In Swift 3, 4 try this:

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 2 try this,

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-C I use this,

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

查看更多
不流泪的眼
5楼-- · 2019-01-01 00:02

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;
}
查看更多
孤独寂梦人
6楼-- · 2019-01-01 00:06

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

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:

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
}
查看更多
伤终究还是伤i
7楼-- · 2019-01-01 00:12

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 preferences Just stick to integers. They default to zero when undefined.

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.

查看更多
登录 后发表回答