-3

For instance, I want a logon screen in ViewController1 to be displayed if the app is launched for the first time. If the app is not launched for the first time or the user has already logged in, it will just start from ViewController2. How do I implement this?

James
  • 1,071
  • 1
  • 11
  • 16
  • just use one controller with two different views, one for first lunch, the other for logged in. – Bruce Lee May 11 '15 at 00:29
  • You can also use two view controllers, and implement logic in the app delegate to launch with the correct one (most likely a "first launched" key-value pair in NSUserDefaults). Having one view controller for loading both the login view and the main view violates the single responsibility principle. – Will M. May 11 '15 at 00:41

3 Answers3

2

Try this link: Check for first launch of my application

The best code for this link was done by Omar, in my opinion. But there are a few great answers on there, as well as one answer with good philosophy on the matter. If you want a small explanation on how to run the code and understand it yourself, NSGod has a good code snippet for you.

If that doesn't help, try this link: How to setup first time launch of an iOS App

ALSO, Here's some code I whipped up for you . . .

- (BOOL)isFirstTimeLaunching {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if([defaults integerForKey:@"hasRun"] == 0) {

        // Do some first-launch code...

        [defaults setInteger:1 forKey:@"hasRun"];
        [defaults synchronize];
        return YES;
    }
    return NO;
}

Remember, returning 0 is returning true. Returning anything other than 0 is returning false. When the app hasn't been run yet, the key @"hasRun" will be false, and will return 0. Once it's been run, reset it back to 0 to prevent the first-launch code from running again.

Community
  • 1
  • 1
Christian Kreiter
  • 610
  • 1
  • 5
  • 16
1

I would use NSUserDefaults. In the AppDelegate.m do something like:

if(![[NSUserDefaults standardDefaults] objectForKey:@"shouldLaunchSignUp"]){

    //launch your sign up view controller here

    //set the user default to yes

    [[NSUserDefaults standardDefaults] setBool:YES forKey:@"shouldLanuchSignUp"];

}
1

check in viewDidLoad and set a boolean flag.

- (BOOL)isFirstTimeLaunching {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if([defaults integerForKey:@"hasRun"] == 0) {

        // Do some first-launch code...

        [defaults setInteger:1 forKey:@"hasRun"];
        [defaults synchronize];
        return YES;
    }


    return NO;
}

And in viewDidAppear, on basis of flag present your log in view.

Remember , in viewDidLoad you can't present another view.

Will M.
  • 1,864
  • 17
  • 28
Anoop
  • 409
  • 3
  • 13