Search code examples
iosswiftgoogle-signingidsignin

Swift: Go to other view controller after Google Sign In


After users successfully signing in, I want the screen to show the tab controller view automatically.

Now I finished the integrating Google Sign In part. But after signing in, the view return to the initial View Controller.

My storyboard looks like this, the blue View inside the initial View Controller is the Google Sign In button.

Below's my didSignInFor function:

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
          withError error: Error!) {
    if let error = error {
        print("\(error.localizedDescription)")
    } else {
        //...
    }
}

I know I should add codes in else{}, but still not sure how to do.

Thanks for help!


Solution

  • For your case first of all you need to create a class for your UITabBarController which will confirm UITabBarController not UIViewController something like:

    class TabBarConroller: UITabBarController {
    

    TabBarConroller is your new .swift file. Now go to your storyboard and click on your TabBarController and click on Identity Inspector and assign this newly created class to it.

    Next you need to initiate that class if user successfully authenticate with below code:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let tabbarVC = storyboard.instantiateViewController(withIdentifier: "TabbarIdentifier") as! UITabbarController
    self.present(tabbarVC, animated: false, completion: nil)
    

    And one more thing you need to assign in Storyboard ID from Identity Inspector which will be TabbarIdentifier.

    So your code will look like:

    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
          withError error: Error!) {
        if let error = error {
            print("\(error.localizedDescription)")
        } else {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let tabbarVC = storyboard.instantiateViewController(withIdentifier: "TabbarIdentifier") as! UITabbarController
            self.present(tabbarVC, animated: false, completion: nil)
        }
    }