How to detect Apps first launch in iOS?

前端 未结 7 894
小蘑菇
小蘑菇 2020-12-01 06:11

I would like to display a welcome screen when a user opens my app for the first time. What method is there to check the first launch of an app in Swift?

相关标签:
7条回答
  • 2020-12-01 06:25

    Try this for Swift 2 and below

    func isAppAlreadyLaunchedOnce()->Bool{
        let defaults = NSUserDefaults.standardUserDefaults()
    
        if let isAppAlreadyLaunchedOnce = defaults.stringForKey("isAppAlreadyLaunchedOnce"){
            println("App already launched")
            return true
        }else{
            defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce")
            println("App launched first time")
            return false
        }
    }
    
    0 讨论(0)
  • 2020-12-01 06:27

    Objective-C version of Jon Shier's answer:

    BOOL isAppLaunched = [[NSUserDefaults standardUserDefaults] boolForKey:@"launchedBefore"];
    if (isAppLaunched) {
        NSLog(@"Not first launch.");
    }
    else {
        NSLog(@"First launch, setting NSUserDefault.");
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"launchedBefore"];
    }
    
    0 讨论(0)
  • 2020-12-01 06:29

    SWIFT :

    let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
    if launchedBefore  {
        print("Not first launch.")
    } else {
        print("First launch, setting UserDefault.")
        UserDefaults.standard.set(true, forKey: "launchedBefore")
    }
    

    OBJECTIVE - C :

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isAppAlreadyLaunchedOnce"])
    {
        return true;
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isAppAlreadyLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return false;
    }
    
    0 讨论(0)
  • 2020-12-01 06:30

    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.

    0 讨论(0)
  • 2020-12-01 06:33

    Since NSUserDefaults for an app are erased when uninstalling the app, you could try testing for the existence of a certain value when the app launches.

    If the value exists, the app had already been installed. If not, this is the first time the app is launched, and you set that value.

    0 讨论(0)
  • 2020-12-01 06:41

    A slight semantic modification of Jonas Deichelmann's answer, clarifying a few things:

    • The function name "establishUserDefaultsHaveBeenVerifed" provides a suggestion that the userDefaults aren't just being checked, but may also be written to.
    • The key previously suggested, "isAppAlreadyLaunchedOnce", describes something the function itself has no control over; that key will only correctly describe the launch state if the function is called at the right time by other code, which again, the function itself can't control. Using the key "userDefaultsHaveBeenVerified" instead makes clear that the only thing the function verifies is that it itself has been run before.
    • The print statements make clear that this function is only verifying if it has been run since last installation, not whether or not it has ever been run on this device.

      func establishUserDefaultsHaveBeenVerifed()->Bool{
           let defaults = UserDefaults.standard
           if let _ = defaults.string(forKey: "userDefaultsHaveBeenVerified"){
               print("user defaults were already verified")
               return true
            }else{
               defaults.set(true, forKey: "userDefaultsHaveBeenVerified")
               print("verified user defaults for first time since app was installed")
               return false
            }
       }
      
    0 讨论(0)
提交回复
热议问题