How many times have you wanted to set an initial view controller in the storyboard conditionally? For example, when building an app that allows user to sign in, you would like to open the login controller the first time, but go right to the authenticated screens everytime the user opens the app in the future. You could do it by creating a dummy initial view controller in the storyboard that just imitates the splash screen and then decide where to go from there. This tutorial explains a way of getting it to work without setting up a dummy controller.

Without any initial view controller in Storyboard

Ensure all controllers that you want as initial view controllers have a Storyboard ID.

Sidrj ldxdx9

In the storyboard, uncheck the Is initial View Controller attribute from the first view controller (or delete the small arrow pointing to your first view controller).

If you run your app at this point you’ll read:

Failed to instantiate the default view controller for UIMainStoryboardFile 'Main.Storyboard' - perhaps the designated entry point is not set?

In the app’s setting, go to your target and the Info tab. Clear the value of Main storyboard file base name. On the General tab, clear the value for Main Interface. This will remove the warning.

Create the window and desired initial view controller in the app delegate’s application:didFinishLaunchingWithOptions: method:

self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

if ([self isUserLoggedIn]) {
    // Show the dashboard
    self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"SDDashboardViewController"];
} else {
    // Login
    self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"SDLoginViewController"];
}
[self.window makeKeyAndVisible];

With UINavigationController as the initial view controller in storyboard

If all your controllers have a navigation bar (or you are willing to manage navigation bar visibility logic in the controllers), an alternative (and a much easier) way to set the initial view controller conditionally is by using a UINavigationController as the initial view controller in the storyboard.

Set up your storyboard with a UINavigationController as the initial view controller and create your initial view controllers with Storyboard IDs but without any link form the UINavigationController.

Screen Shot 2015 04 17 at 16 37 18 asvg5j

Now, somewhere in your delegate’s application:didFinishLaunchingWithOptions: method, instantiate the proper view controller and push it to the navigation controller.

var navigationController: UINavigationController? = (self.window?.rootViewController as? UINavigationController)
var storyboard = UIStoryboard(name: "Main", bundle: nil)
if self.isUserLoggedIn() {
    // Show the dashboard
    navigationController?.pushViewController(storyboard.instantiateViewController(withIdentifier: "SDDashboardViewController"), animated: false)
}
else {
    // Login
    navigationController?.pushViewController(storyboard.instantiateViewController(withIdentifier: "SDLoginViewController"), animated: false)
}