New iOS developer. I am working on a project that requires the user to be logged in the first time the application is opened. From now on, I want the application to open directly in the main thread of the application (in my case, the controller of the tab bar). After doing some research, I came across what seems to be the two main ways to implement this function:
1) Conditionally install the root view controller of the application window in the application delegate. For example:
if userLoggedIn { let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) let tabBarController: UITabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController self.window?.makeKeyAndVisible() self.window?.rootViewController = tabBarController } else { let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) let logInViewController: LogInViewController = storyboard.instantiateViewControllerWithIdentifier("LogInViewController") as! LogInViewController self.window?.makeKeyAndVisible() self.window?.rootViewController = logInViewController }
2) Use the navigation controller as the view controller of the root application and conditionally configure the view controllers controlled by the navigation controller in the application delegate. For example:
if userLoggedIn { let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) let tabBarController: UITabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController let navigationController = self.window?.rootViewController as! UINavigationController navigationController.navigationBarHidden = true navigationController.setViewControllers([tabBarController], animated: true) } else { let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) let tabBarController: ViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController") as! ViewController let navigationController = self.window?.rootViewController as! UINavigationController navigationController.navigationBarHidden = true navigationController.setViewControllers([tabBarController], animated: true) }
If I go to the second option, I can easily go to the main thread of the application (say, the tab bar controller) after I finish logging in. In the appropriate place in the LogInViewController I can say:
// Transition to tab bar controller let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) let tabBarController: UITabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController self.navigationController?.setViewControllers([tabBarController], animated: true)
Pretty easy.
Regarding the first method, I'm not sure how to go to the main content of the application after the user logs in.
What I'm looking for here is the "best" way to handle the input stream. I have listed two methods, but maybe there is one more that is even better. Also, if the βbestβ method is the first one I have listed, how can I get to my tab bar controller after I finish user registration? Thanks!