Conditionally start at different places in storyboard from AppDelegate Conditionally start at different places in storyboard from AppDelegate objective-c objective-c

Conditionally start at different places in storyboard from AppDelegate


I'm surprised at some of the solutions being suggested here.

There's really no need for dummy navigation controllers in your storyboard, hiding views & firing segues on viewDidAppear: or any other hacks.

If you don't have the storyboard configured in your plist file, you must create both the window and the root view controller yourself :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{            BOOL isLoggedIn = ...;    // from your server response    NSString *storyboardId = isLoggedIn ? @"MainIdentifier" : @"LoginIdentifier";    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];    UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:storyboardId];    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    self.window.rootViewController = initViewController;    [self.window makeKeyAndVisible];    return YES;}

If the storyboard is configured in the app's plist, the window and root view controller will already be setup by the time application:didFinishLaunching: is called, and makeKeyAndVisible will be called on the window for you.

In that case, it's even simpler:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{            BOOL isLoggedIn = ...;    // from your server response    NSString *storyboardId = isLoggedIn ? @"MainIdentifier" : @"LoginIdentifier";    self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:storyboardId];    return YES;}


I assume your storyboard is set as the "main storyboard" (key UIMainStoryboardFile in your Info.plist). In that case, UIKit will load the storyboard and set its initial view controller as your window's root view controller before it sends application:didFinishLaunchingWithOptions: to your AppDelegate.

I also assume that the initial view controller in your storyboard is the navigation controller, onto which you want to push your main or login view controller.

You can ask your window for its root view controller, and send the performSegueWithIdentifier:sender: message to it:

NSString *segueId = success ? @"pushMain" : @"pushLogin";[self.window.rootViewController performSegueWithIdentifier:segueId sender:self];


IF your storyboard's entry point isn't an UINavigationController:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    //Your View Controller Identifiers defined in Interface Builder    NSString *firstViewControllerIdentifier  = @"LoginViewController";    NSString *secondViewControllerIdentifier = @"MainMenuViewController";    //check if the key exists and its value    BOOL appHasLaunchedOnce = [[NSUserDefaults standardUserDefaults] boolForKey:@"appHasLaunchedOnce"];    //if the key doesn't exist or its value is NO    if (!appHasLaunchedOnce) {        //set its value to YES        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"appHasLaunchedOnce"];        [[NSUserDefaults standardUserDefaults] synchronize];    }    //check which view controller identifier should be used    NSString *viewControllerIdentifier = appHasLaunchedOnce ? secondViewControllerIdentifier : firstViewControllerIdentifier;    //IF THE STORYBOARD EXISTS IN YOUR INFO.PLIST FILE AND YOU USE A SINGLE STORYBOARD    UIStoryboard *storyboard = self.window.rootViewController.storyboard;    //IF THE STORYBOARD DOESN'T EXIST IN YOUR INFO.PLIST FILE OR IF YOU USE MULTIPLE STORYBOARDS    //UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YOUR_STORYBOARD_FILE_NAME" bundle:nil];    //instantiate the view controller    UIViewController *presentedViewController = [storyboard instantiateViewControllerWithIdentifier:viewControllerIdentifier];    //IF YOU DON'T USE A NAVIGATION CONTROLLER:    [self.window setRootViewController:presentedViewController];    return YES;}

IF your storyboard's entry point IS an UINavigationController replace:

//IF YOU DON'T USE A NAVIGATION CONTROLLER:[self.window setRootViewController:presentedViewController];

with:

//IF YOU USE A NAVIGATION CONTROLLER AS THE ENTRY POINT IN YOUR STORYBOARD:UINavigationController *navController = (UINavigationController *)self.window.rootViewController;[navController pushViewController:presentedViewController animated:NO];