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?
-
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 Answers
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.

- 1
- 1

- 610
- 1
- 5
- 16
-
Thank you so much but how do I set the initial view to load in AppDelegate.m according to the return value of this isFirstTimeLaunching method? – James May 11 '15 at 03:59
-
Try this: http://stackoverflow.com/questions/10428629/programatically-set-the-initial-view-controller-using-storyboards – Christian Kreiter May 11 '15 at 13:31
-
Thanks a lot. I don't know why so many people downvoted my question. I'm really new to iOS dev. – James May 11 '15 at 13:49
-
It may be that they weren't too fond of your possible repetition of other discussions. – Christian Kreiter May 11 '15 at 17:44
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"];
}

- 21
- 4
-
Just use `setBool:` on the defaults, no need to manually construct an NSNumber. – Jon Shier May 11 '15 at 01:16
-
Ahh yeah, you are correct. I had the NSNumber way of thinking due to dealing with Parse lately. I will edit the answer. – Brad Hughes May 11 '15 at 07:46
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.