0

I have the following code where I'm trying to push the Home2ViewController (Tab Bar Controller) so the user will be redirected to this after his Google Sign In procedure:

let viewController: Home2ViewController = storyboard.instantiateViewController(withIdentifier: "HomeVC") as! Home2ViewController;

// error occurs on the following line
let rootViewController = self.window!.rootViewController as! UINavigationController;
rootViewController.pushViewController(viewController, animated: true);

I'm receiving an error saying:

Unexpectedly found nil while unwrapping an Optional value

on the second line of code where I'm defining the rootViewController.

Home2ViewController is a subclass of UITabBarController and is identified with HomeVC in the storyboard.

How could I solve this error so I can have the right controller being shown after the Google Sign In procedure?

Edit: Here is the full code of the Google Sign In method from the AppDelegate.

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if let err = error {
            print("Failed to log into Google", err)
            return
        }

        guard let idToken = user.authentication.idToken else {return}
        guard let accessToken = user.authentication.accessToken else {return}

        let credentials = GoogleAuthProvider.credential(withIDToken: idToken,accessToken: accessToken)
        print("Successfully logged into Google", user)

        Auth.auth().signInAndRetrieveData(with: credentials,  completion: { (user, error) in
            if let err = error {
                print("Failed to create a Firebase user with Google account", err)
                return

            }
            print("Successfully logged into Firebase with Google")

            let storyboard = UIStoryboard(name: "Main", bundle: nil);
            guard let initialViewController = storyboard.instantiateViewController(withIdentifier: "HomeVC") as? Home2ViewController else { return }
            self.window?.rootViewController = initialViewController
            self.window?.makeKeyAndVisible()

        })        
    }

shim
  • 9,289
  • 12
  • 69
  • 108
  • Have you checked to make sure `self.window!.rootViewController` is actually a `UINavigationController`? Based on the error and where you're saying it's occurring that seems to be the problem. – shim Apr 05 '20 at 19:56

1 Answers1

0

This might sounds silly , just make sure the identifier name is exactly the same & if that didn't solve the issue change it to something else in storyBoard and code , it happened to me once & that what solved the issue