I achieve this with the “LaunchViewController”, which determines whether it should represent the “Input” view or the main navigation controller.
Mark the initial run in the storyboard and write the logic there to determine what to represent. This process is often very fast and invisible to the user.
UPDATE
As mentioned in my comment, I went in a different direction. Now in the App Delegate application(application:didFinishLaunchingWithOptions:) I follow the logic needed to determine what kind of rootViewController should be and set it there using the following.
extension AppDelegate { enum LaunchViewController { case Login, Dashboard var viewController: UIViewController { switch self { case .Login: return StoryboardScene.Login.LoginScene.viewController() case .Dashboard: return StoryboardScene.Dashboard.initialViewController() } } /// Sets `UIWindow().rootViewController` to the appropriate view controller, by default this runs without an animation. func setAsRootviewController(animated animated: Bool = false) { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let window = appDelegate.window! let launchViewController = viewController log.info?.message("Setting \(launchViewController.dynamicType) as rootViewController") if let rootViewController = window.rootViewController where rootViewController.dynamicType != launchViewController.dynamicType && animated { let overlayView = UIScreen.mainScreen().snapshotViewAfterScreenUpdates(false) launchViewController.view.addSubview(overlayView) UIView.animateWithDuration(0.3, animations: { overlayView.alpha = 0.0 }, completion: { _ in overlayView.removeFromSuperview() }); } window.rootViewController = launchViewController window.restorationIdentifier = String(launchViewController.dynamicType) if window.keyWindow == false { window.makeKeyAndVisible() } } } }
Please note that StoryboardScene not the default type; I use https://github.com/AliSoftware/SwiftGen to create it.
My application delegation method looks something like this.
if window == nil { window = UIWindow(frame: UIScreen.mainScreen().bounds) } if isRestoringState == false { if let _ = lastUsedAccount { LaunchViewController.Dashboard.setAsRootviewController() } else { LaunchViewController.Login.setAsRootviewController() } }
UPDATE with exit method for comments
func handleLogout(notification: NSNotification) { LaunchViewController.Login.setAsRootviewController(animated: true) lastUsedAccount = nil }
source share