1

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?

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Kumar
  • 1,882
  • 2
  • 27
  • 44
  • 1
    You could use ```NSUserDefaults``` to store a string value which states if the user has used your app before or not. –  Jul 09 '15 at 09:07
  • 1
    possible duplicate of [How to detect an iOS App installed or upgraded?](http://stackoverflow.com/questions/22118943/how-to-detect-an-ios-app-installed-or-upgraded) – streem Jul 09 '15 at 09:11

4 Answers4

5

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.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
2

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.

Nikita Zernov
  • 5,465
  • 6
  • 39
  • 70
1

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.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Fawad Masud
  • 12,219
  • 3
  • 25
  • 34
0

you need to use boolForKey and setBool

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