Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I change the initial view controller programmatically?

I am trying to lead the user directly to the main page of my application if they are logged in (checked using user defaults).

However, it will stay at the screen I chose as the initial view controller in the storyboard no matter what. This is my didFinishLaunchingWithOptions function in the AppDelegate file:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        
        
        let db = Firestore.firestore()
        print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true))
        if UserDefaults.standard.string(forKey: "userEmail") != nil {
            self.window = UIWindow(frame: UIScreen.main.bounds)
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewController = storyboard.instantiateViewController(withIdentifier: "TabBarController")
            let navigationController = UINavigationController(rootViewController: initialViewController)
            self.window?.rootViewController = navigationController
            self.window?.makeKeyAndVisible()

        }
        return true
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

From iOS 13 on, the responsibilites of AppDelegate have been split between AppDelegate and SceneDelegate. In particular, SceneDelegate is where you want to present a UIViewController.

Remove the if block of your code from AppDelegate and add it to wilConnectTo of SceneDelegate, like this:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    guard let windowScene = (scene as? UIWindowScene) else { return }

    if UserDefaults.standard.string(forKey: "userEmail") != nil {
        self.window = UIWindow(windowScene: windowScene)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController = storyboard.instantiateViewController(identifier: "TabBarController")
        let navigationController = UINavigationController(rootViewController: initialViewController)
        self.window?.rootViewController = navigationController
        self.window?.makeKeyAndVisible()
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading