ivan.vliza
ivan.vliza

Reputation: 183

Setting root view controller upon launch, depending on user auth state

This is the storyboard layout I have right now

enter image description here

So what I need is to check if the user is already authenticated when the app launches. If he is not logged in set the root view controller to the navigation controller with the login and register forms. If he is logged in set it to the tab bar view controller.

I tried a lot of different solutions but none of them worked. It just kept setting the view controller to the one marked with "Is Initial View Controller".

This is the code I tried in the AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    FirebaseApp.configure()

    self.window = UIWindow(frame: UIScreen.main.bounds)

    let storyboard = UIStoryboard.init(name: "Main", bundle: nil)

    var viewController: UIViewController

    if isAuthenticated() {
        viewController = storyboard.instantiateViewController(withIdentifier: Constants.Storyboards.homeViewController) as! UITabBarController
    } else {
        viewController = storyboard.instantiateViewController(withIdentifier: Constants.Storyboards.authViewController) as! UINavigationController
    }

    self.window?.rootViewController = viewController
    self.window?.makeKeyAndVisible()

    return true
}

I can do this by pressing a button easily, but I want it to happen without the user needing to do something.

EDIT: Thanks to @LukaCefarin and @Francesco Deliro I managed to find out that the problem was. I was using XCode 11 and the rootViewController had to be set in the SceneDelegate.swift

This is what my code in the SceneDelegate.swift looks like for anyone who has a similar issue:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    guard let _ = (scene as? UIWindowScene) else { return }

    self.window =  UIWindow(windowScene: scene as! UIWindowScene)

    let storyboard = UIStoryboard.init(name: "Main", bundle: nil)

    var viewController: UIViewController;

    if isAuthenticated() {
        viewController = storyboard.instantiateViewController(withIdentifier: "HomeVC")
    } else {
        viewController = storyboard.instantiateViewController(withIdentifier: "AuthVC")
    }

    self.window?.rootViewController = viewController
    self.window?.makeKeyAndVisible()

}

Upvotes: 0

Views: 493

Answers (1)

Francesco Deliro
Francesco Deliro

Reputation: 3939

EDIT:

This solution and setting the window rootViewController in the AppDelegate work for versions prior to Xcode11 and iOS13. As suggested in the comments by Luka Cefarin, if you are using Xcode11 and iOS13 you have to set the window rootViewController in the SceneDelegate.swift file.

You have to remove the Main Interface and to uncheck the initial view controller in the storyboard:

Before

enter image description here

After

enter image description here

Upvotes: 1

Related Questions