1

First shows AuthViewController (aka root) then a black screen with tabBarController, but without item on it.

What could be the problem?

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

        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.windowScene = windowScene

        if let user = Auth.auth().currentUser {
            FirestoreServices.shared.getUserData(user: user) { (result) in
                switch result {
                case .success(let muser):
                    let mainTabBar = MainTabBarViewController()
                    mainTabBar.currentUser = muser
                    mainTabBar.modalPresentationStyle = .fullScreen
                    self.window?.rootViewController = mainTabBar
                case .failure(_):
                    self.window?.rootViewController = AuthViewController()
                }
            }
        } else {
            self.window?.rootViewController = AuthViewController()
        }
        window?.makeKeyAndVisible()
    } 

1 Answers1

2

EDIT: You need to instantiate from storyboard since you are using storyboard. You need to initialize your window variable window with windowScene.

var window: UIWindow?

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

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

        window = UIWindow(windowScene: windowScene)
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        if let user = Auth.auth().currentUser {
            FirestoreServices.shared.getUserData(user: user) { (result) in
                switch result {
                case .success(let muser):
                    let mainTabBar = storyboard.instantiateViewController(withIdentifier: "viewControllerIDInStoryboard") as! TabViewController
                    mainTabBar.currentUser = muser
                    mainTabBar.modalPresentationStyle = .fullScreen
                    self.window?.rootViewController = mainTabBar
                    print("BLA BLA \(String(describing: self.window?.frame))")
                case .failure(_):
                    let authVC = storyboard.instantiateViewController(withIdentifier: "viewControllerIDInStoryboard") as! AuthViewController
                    self.window?.rootViewController = authVC
                }
            }
        } else {
            let authVC = storyboard.instantiateViewController(withIdentifier: "viewControllerIDInStoryboard") as! AuthViewController
            self.window?.rootViewController = authVC
        }
        window?.makeKeyAndVisible()
    } 
rs7
  • 1,618
  • 1
  • 8
  • 16
  • I updated the code in the post Your answer didn’t help me ( – Евгений Jun 13 '20 at 01:38
  • This works if you don’t use a storyboard, but I need to use a storyboard – Евгений Jun 13 '20 at 01:50
  • If you use storyboard, the first code will work. I just tested it. Make sure you don't have any ViewController in storyboard that is set as the initial View Controller (no arrow should be pointing to it) – rs7 Jun 13 '20 at 01:56
  • 1
    Removed the arrow from the storyboard, but the problem did not disappear, now it shows a black screen and after half a second shows a black screen with TabBar – Евгений Jun 13 '20 at 02:11
  • Okay, I've updated my answer. If you use storyboard, you need to instantiate your ViewControllers from storyboard. – rs7 Jun 13 '20 at 02:18
  • **initialize your window variable window with windowScene.** This was the trick. Many thanks – Asawari Jul 09 '23 at 23:25