ios - 在 swift 中使用 google 集成时没有生成多个用户标识

标签 ios swift google-signin

我有一个与 google 、 twitter 和 Facebook 集成的应用程序。twitter 和 Facebook 没有问题,但是当我使用 google 集成时,它总是返回相同的 userId 。

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

        let profileUserId = user.userID
        let idToken = user.authentication.idToken
        let fullName = user.profile.name
        let givenName = user.profile.givenName
        let familyName = user.profile.familyName
        let email = user.profile.email

        let urlString : String?
        if user.profile.hasImage
        {
            let pic = user.profile.imageURL(withDimension: 100)
            urlString = appDelegate.serverUrl + "socialsignup?&full_name=\(fullName!)&username=\(givenName!)&email=\(email!)&user_image_name=\(pic!)&google_id=\(profileUserId!)"

        }
        else
        {
            urlString = appDelegate.serverUrl + "socialsignup?&full_name=\(fullName!)&username=\(givenName!)&email=\(email!)&user_image_name=&google_id=\(profileUserId!)"
        }

这是我的谷歌委托(delegate)方法。我的代码有什么问题吗?

最佳答案

注销之前的用户

在登录按钮中你必须这样调用

let sighIn:GIDSignIn = GIDSignIn.sharedInstance()
    sighIn.delegate = self;
    sighIn.uiDelegate = self;
    sighIn.shouldFetchBasicProfile = true
    sighIn.scopes = ["https://www.googleapis.com/auth/plus.login","https://www.googleapis.com/auth/userinfo.email","https://www.googleapis.com/auth/userinfo.profile","https://www.googleapis.com/auth/plus.me"];
    sighIn.clientID = "your client id"
    sighIn.signIn()
    GIDSignIn.sharedInstance().signOut()

那么剩下的方法应该是这样的

   func sign(_ signIn: GIDSignIn!, present viewController: UIViewController!) {
    self.present(viewController, animated: true, completion: nil)
}

func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!) {
    self.dismiss(animated: true, completion: nil)
}

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
    if (error != nil) {
        return
    }
    reportAuthStatus()
    if GIDSignIn.sharedInstance().currentUser.profile.hasImage {
        imageURL = user.profile.imageURL(withDimension: UInt(120)) as NSURL
        print("Image Url : \(imageURL)")
    }
    userdata = user
    googleID = user.userID
    let login_with = "google"
    DeviceType = "ios"
    self.TokenStr = user.authentication.idToken
    let fullName = user.profile.name
    let GivenName = user.profile.givenName
    let FamilyName = user.profile.familyName
    let email = user.profile.email
    print("user details : \(GivenName,fullName,FamilyName,email,googleID)")

 }
 func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
    if (error != nil) {

    } else {
      }
}
func reportAuthStatus() -> Void {
    let googleUser:GIDGoogleUser = GIDSignIn.sharedInstance().currentUser
    if (googleUser.authentication != nil) {
        print("Status: Authenticated")
    } else {
        print("Status: Not authenticated")
    }
 }
func refreshUserInfo() -> Void {
    if GIDSignIn.sharedInstance().currentUser.authentication == nil {
        return
    }
    if !GIDSignIn.sharedInstance().currentUser.profile.hasImage {
        return
    }
}

试试这个,我希望你能得到输出

关于ios - 在 swift 中使用 google 集成时没有生成多个用户标识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51076523/

相关文章:

ios - 使用 UITextField 为单元格的 UITableView 启用滚动

ios - CoreBluetooth 框架可以与从蓝牙 2.0 串行模块配对并交谈吗?

ios - Swift:在 didSelect View 中更改图钉颜色:MKAnnotationView

ios - 为什么我选择单元格时字体没有改变?

ios google登录,如何获取用户图片网址?

ios - UIscrollView 快速分页和缩放图像

ios - 如何在按钮上设置图像

ios - 无法更改 TableView 单元格的行高 - Swift

javascript - 使用 AngularJS 将 Google 登录添加到您的网站

android - 使用谷歌登录 `gms.auth.UserRecoverableAuthException: BadAuthentication` 异常 android