Swift: Go to other view controller after Google Sign In

2.9k Views Asked by At

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!

3

There are 3 best solutions below

3
Dharmesh Kheni On BEST ANSWER

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)
    }
}
2
Deviyani Swami On
var loginType : String = ""
var tokenFb : String = ""
var email : String = ""
var googleName : String = ""
// Google Integration

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

        self.loginType = "gmail"
        // Perform any operations on signed in user here.
        tokenFb = user.userID // For client-side use only!
        let idToken = user.authentication.idToken // Safe to send to the server
        googleName = user.profile.name
        let givenName = user.profile.givenName
        let familyName = user.profile.familyName
        email = user.profile.email

        if user.profile.hasImage
        {
            let imageUrl = signIn.currentUser.profile.imageURL(withDimension: 150)

            image = (imageUrl?.absoluteString)!
            print(" image url: ", image)
            self.profileURL = image
        }
        self.loginSocialAPI()
    }
}

self.logSocialApi() is the API through which I can login and in that api need to set the path of particular viewController.

0
catmember On

If anyone is still having this problem, I used this code to redirect to a different view controller:

    // redirects to signed in user view controller
        let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = mainStoryboard.instantiateViewController(withIdentifier: "User_Feed_View")
        UIApplication.shared.windows.first?.rootViewController? = vc
        UIApplication.shared.windows.first?.makeKeyAndVisible()

I used this in appDelegate inside the didSignInFor sign function, and it seems to work.

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
              withError error: Error!) {
      if let error = error {
        if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue {
          print("The user has not signed in before or they have since signed out.")
        } else {
          print("\(error.localizedDescription)")
        }
        return
      }
      // Perform any operations on signed in user here.
      let userId = user.userID                  // For client-side use only!
      let idToken = user.authentication.idToken // Safe to send to the server
      let fullName = user.profile.name
      let givenName = user.profile.givenName
      let familyName = user.profile.familyName
      let email = user.profile.email
     
        
        
        // redirects to signed in user's view controller
        let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = mainStoryboard.instantiateViewController(withIdentifier: "User_Feed_View")
        UIApplication.shared.windows.first?.rootViewController? = vc
        UIApplication.shared.windows.first?.makeKeyAndVisible()
        
    }