The best way to conditionally switch the root view controller when launching an iOS application

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!

+7
ios login swift
source share
1 answer

These are great options. One thing that I worked with has worked well, is the custom container view controller as the root of my application (this could be a subclass of my main view controller, such as a tab bar controller or navigation controller).

In this container view controller, I can put code to display and reject the login controller based on the user's login state. Note. You can also use temporary full-screen views here to hide the main content of your application when the login screen is displayed / rejected - this is an easy way to achieve a very smooth transition to launching the application.

I especially like this method because the rest of the applications do not need to worry about the details. Your container can act as a regular tab bar controller or navigation controller, but under it you can encapsulate all the login logic in one place.

+8
source share

All Articles