0

I want to set root view controller after user login successfully. I implement below code and work well on below iOS 13 but when I run in iOS 13 simulator it's not work for me. Please guide how to set root view controller which work below and above iOS 13.

    let navigationVC = UIStoryboard.dashboard.instantiateViewController(withIdentifier: "DashboardNavigation") as! UINavigationController
    let loginVC = UIStoryboard.dashboard.instantiateViewController(withIdentifier: "HomeVC")
    navigationVC.viewControllers = [loginVC]
    Application.window?.rootViewController = navigationVC
Krunal Nagvadia
  • 1,083
  • 2
  • 12
  • 33

2 Answers2

0

Try this.

let appDelegate = UIApplication.shared.delegate! as! AppDelegate
let initialViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeVC") as! HomeVC
appDelegate.window?.rootViewController?.view.removeFromSuperview()
appDelegate.window?.rootViewController = initialViewController
appDelegate.window?.makeKeyAndVisible()
Sami Ali
  • 163
  • 3
  • 10
0

i checked below code in both Real device as well as simulator it works fine in both iOS 13 and Devices Running Below iOS 13

func setInitialVC(){
    
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    var initialVc:UIViewController?
    let storyboard=UIStoryboard(name: "Main", bundle: nil)
    
    If (condition){
        let homeVC = storyboard.instantiateViewController(withIdentifier: “HomeViewControllerSegue”)
        initialVc = homeVC
    }
    else{
        let loginVC = storyboard.instantiateViewController(withIdentifier: “LoginViewControllerSegue”)
        initialVc = loginVC
    }
    
    if #available(iOS 13.0, *) {
        if let scene = UIApplication.shared.connectedScenes.first{
            guard let windowScene = (scene as? UIWindowScene) else { return }
            let window: UIWindow = UIWindow(frame: windowScene.coordinateSpace.bounds)
            window.windowScene = windowScene
            window.rootViewController = initialVc
            window.makeKeyAndVisible()
            appDelegate.window = window
        }
    }
    else
    {
        appDelegate.window?.rootViewController = initialVc
        appDelegate.window?.makeKeyAndVisible()
    }
}

Note:Pls Dont Forget to Add var window: UIWindow? in your AppDelegate Class

Gowtham
  • 385
  • 1
  • 11
  • It'd be helpful if you could explain what difference makes your code work where the OP's fails. – Caleb Dec 24 '20 at 07:48