IOS | Unable to change root view controller in app delegate

前端 未结 2 983
误落风尘
误落风尘 2020-12-04 02:59

I am building an app whose root view controller must be changed if the user is logged in. Say if the user is logged in I must show a tab bar controller as the home screen if

相关标签:
2条回答
  • 2020-12-04 03:41

    If you are targeting only iOS 13+, the only change you should need to make is to add one line:

        window?.rootViewController = initialViewController
    
        // add this line
        self.window = window
    
        window?.makeKeyAndVisible()
    

    If you want to support earlier iOS versions, here is a complete SceneDelegate / AppDelegate implementation:

    SceneDelegate.swift

    //
    //  SceneDelegate.swift
    //  Created by Don Mag on 3/27/20.
    //
    
    import UIKit
    
    // entire class is iOS 13+
    @available(iOS 13.0, *)
    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
        var window: UIWindow?
    
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
            print("Scene Delegate willConnectTo", UserDefaults.standard.bool(forKey: "isLoggedIn"))
    
            guard let windowScene = (scene as? UIWindowScene) else { return }
            let window = UIWindow(frame: windowScene.coordinateSpace.bounds)
            window.windowScene = windowScene
    
            if UserDefaults.standard.bool(forKey: "isLoggedIn") {
                guard let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeVC") as? TabController else {
                    fatalError("Could not instantiate HomeVC!")
                }
                window.rootViewController = vc
            } else {
                guard let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AuthVC") as? AuthViewController else {
                    fatalError("Could not instantiate HomeVC!")
                }
                window.rootViewController = vc
            }
    
            self.window = window
    
            window.makeKeyAndVisible()
        }
    
    }
    

    AppDelegate.swift

    //
    //  AppDelegate.swift
    //  Created by Don Mag on 3/27/20.
    //
    
    import UIKit
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window : UIWindow?
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
                if #available(iOS 13, *) {
                    // do only pure app launch stuff, not interface stuff
                } else {
    
                    print("App Delegate didFinishLaunching... isLoggedIn:", UserDefaults.standard.bool(forKey: "isLoggedIn"))
    
                    self.window = UIWindow()
    
                    if UserDefaults.standard.bool(forKey: "isLoggedIn") {
                        guard let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeVC") as? TabController else {
                            fatalError("Could not instantiate HomeVC!")
                        }
                        window?.rootViewController = vc
                    } else {
                        guard let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AuthVC") as? AuthViewController else {
                            fatalError("Could not instantiate HomeVC!")
                        }
                        window?.rootViewController = vc
                    }
    
                    window?.makeKeyAndVisible()
    
                }
                return true
        }
    
        // MARK: UISceneSession Lifecycle
    
        // iOS 13+ only
        @available(iOS 13.0, *)
        func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
            return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
        }
        // iOS 13+ only
        @available(iOS 13.0, *)
        func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        }
    
    }
    
    0 讨论(0)
  • 2020-12-04 03:57

    Here's a way to extend your UIWindow (and access to your rootViewController) into your view controllers to make it easier to change your rootviewcontroller throughout your app:

    extension UIViewController {
        var appDelegate: AppDelegate {
        return UIApplication.shared.delegate as! AppDelegate
    }
    
    var sceneDelegate: SceneDelegate? {
        guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
            let delegate = windowScene.delegate as? SceneDelegate else { return nil }
         return delegate
    }}
       
    
    
    
    
    
    extension UIViewController {   
            var window: UIWindow? {
                if #available(iOS 13, *) {
                    guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
                        let delegate = windowScene.delegate as? SceneDelegate, let window = delegate.window else { return nil }
                           return window
                }
                
                guard let delegate = UIApplication.shared.delegate as? AppDelegate, let window = delegate.window else { return nil }
                return window
            }
        }
    
    0 讨论(0)
提交回复
热议问题