Kumar
Kumar

Reputation: 1942

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?

Upvotes: 1

Views: 1836

Answers (4)

you need to use boolForKey and setBool

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

Upvotes: 0

Fawad Masud
Fawad Masud

Reputation: 12344

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.

Upvotes: 1

Nikita Zernov
Nikita Zernov

Reputation: 5635

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.

Upvotes: 2

Ashish Kakkad
Ashish Kakkad

Reputation: 23882

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.

Upvotes: 5

Related Questions