简体   繁体   中英

Google didSignInForUser doesn't fire in swift 3

I am doing Google sign in in my project. I installed the SDK using CocoaPods. I have this same app written in Objective-C before. So I dragged the same Google configuration file (GoogleService-Info.plist) from my old project.

And I followed this with CocoaPods installed method . But when I press the sign in button I am directing to the Google web page and after I enter user name password it returns to my app. But this delegate doesn't trigger.

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
    if error == nil {
        // 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
        let dm = Datamanager.sharedInstance
        dm.gplusEmail = email
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "RETURNED_GOOGLE"), object: nil)
        // ...
    } else {
        print("\(error.localizedDescription)")
    }
}

My sign in button is a UIButton and in my ViewController I have implemented in this way:

@IBAction func googleClick(_ sender: UIButton) {
    strLoggedWay="google"
    GIDSignIn.sharedInstance().signIn()
}

Please help me. What's wrong with the process my code?

//In your login view controller

@IBAction func googleClick(_ sender: UIButton) {

    strLoggedWay="google"

    GIDSignIn.sharedInstance().clientID = //getGoogleClientId
    GIDSignIn.sharedInstance().serverClientID = //getGoogleServerClientId
    GIDSignIn.sharedInstance().uiDelegate = self
    GIDSignIn.sharedInstance().delegate = self
    GIDSignIn.sharedInstance().scopes = @["https://www.googleapis.com/auth/userinfo.profile","https://www.googleapis.com/auth/userinfo.email"]
    GIDSignIn.sharedInstance().signIn()
}

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
            withError error: NSError!) {
    if (error == nil) {
        // 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
        let dm=Datamanager.sharedInstance
        dm.gplusEmail=email
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "RETURNED_GOOGLE"), object: nil)
        // ...
    } else {
        print("\(error.localizedDescription)")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM