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()
})
}