我有一个与 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/