Swift: Go to other view controller after Google Sign In

守給你的承諾、 提交于 2019-12-24 20:34:21

问题


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!


回答1:


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:


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.



来源:https://stackoverflow.com/questions/55215761/swift-go-to-other-view-controller-after-google-sign-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!