简体   繁体   中英

How to check if user visit my iOS app for the first time?

If user is opening application for the first time after downloading it, then I want to show a view controller with tutorial and then go to main application's View Controller. But if user is already visited app before, then go to Main view controller.

How to implement that check in AppDelegate?

add in your app delegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"firsttime"]) {
        [[NSUserDefaults standardUserDefaults]setObject:@"YES" forKey:@"firsttime"];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }

}

If condition will check for the key is available in app or not.

If it is not available then it will save it to YES.

You can do as you want.

I would do that in your main view controller in method viewWillAppear . You can use NSUserDefaults . Here's an example:

Swift:

func isAppAlreadyLaunchedOnce() {
    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")
        //Show your tutorial.
        return false
    }
}

Objective-C:

- (void)isAppAlreadyLaunchedOnce {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *isAppAlreadyLaunchedOnce = [defaults stringForKey:@"isAppAlreadyLaunchedOnce"];

    if (isAppAlreadyLaunchedOnce != nil) {
        NSLog(@"App already launched");
        return true;
    } else {
        NSLog(@"App launched first time");
        [defaults setBool:true forKey:@"isAppAlreadyLaunchedOnce"];
        //Show your tutorial.
        return false;
    }
}

And then run this method in your viewWillAppear :

Swift:

func viewWillAppear(animated: Bool) {
    isAppAlreadyLaunchedOnce()
}

Objective-C:

- (void)viewWillAppear:(BOOL)animated {
    [self isAppAlreadyLaunchedOnce];
}

NSUserDefaults will be cleared when user will uninstall app.

Add a flag in NSUserDefaults . When the user launches the app for the first time, the flag is not set. After checking the status of the flag, if the flag is not set the flag should be set.

you need to use boolForKey and setBool

if (![[NSUserDefaults standardUserDefaults]boolForKey:@"firsttime"]) {
                [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"firsttime"];
                [[NSUserDefaults standardUserDefaults]synchronize];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM