ios – Swift Google登录GPPSignIn.sharedInstance().userID等于总是为零

我正在尝试使用
Swift语言在iOS应用中实现Google登录.

我的代码看起来像这样:

var kClientId = "My Client ID from Dev Console"
var signIn = GPPSignIn.sharedInstance()
signIn.shouldFetchGooglePlusUser = true
signIn.shouldFetchGoogleUserEmail = true
signIn.shouldFetchGoogleUserID = true
signIn.clientID = kClientId
signIn.scopes = [kGTLAuthScopePlusLogin]
signIn.delegate = self
signIn.authenticate()

出于测试目的,我创建了一个标签,并希望将其更改为登录的用户电子邮件.

if (GPPSignIn.sharedInstance().userID != nil) {
    var user = GPPSignIn.sharedInstance().googlePlusUser
    userName.text = user.name.JSONString()
    if (user.emails != nil){
        userEmailLable.text = user.emails.first?.JSONString() ?? "no email"
    } else {
        userEmailLable.text = "no email"
    }
} else {
    println("User ID is nil")
}

单击“登录”按钮后,Safari选项卡打开,我可以输入我的Google电子邮件和密码,它会询问某些内容的权限,按“接受”按钮后,它将返回到应用程序.我的userEmailLable没有更改,它输出“User ID is nil”作为输出.它一直在发生,并没有一次成功登录.

我的Google框架都很好,网址也是正确的,在Google Developer Console中应该是应有的一切.

AppDelegate.swift文件也包含此功能

func application(application: UIApplication, openURL url: NSURL, sourcApplication: String, annotation: AnyObject?) -> Bool {
    return GPPURLHandler.handleURL(url, sourceApplication: sourcApplication, annotation: annotation)
}

有谁知道它为什么这样做?谢谢!

最佳答案 这很奇怪.我尝试在
Google instructions.之后构建一个示例它按预期工作并打印出正确的输出.

另外,here is another nice step-by-step writeup.

我做了与指南相同的步骤 – 创建一个控制台应用程序,使用我的包ID注册iOS应用程序的URL方案.

这是我的视图控制器代码.

import UIKit

class ViewController: UIViewController, GPPSignInDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let signIn = GPPSignIn.sharedInstance()
        signIn.shouldFetchGooglePlusUser = true
        signIn.shouldFetchGoogleUserEmail = true  // Uncomment to get the user's email
        signIn.shouldFetchGoogleUserID = true

        signIn.clientID = "… my client ID"

        // Uncomment one of these two statements for the scope you chose in the previous step
        signIn.scopes = [ kGTLAuthScopePlusLogin ]  // "https://www.googleapis.com/auth/plus.login" scope

        signIn.delegate = self
        signIn.authenticate()
    }



    // MARK: - GPPSignInDelegate

    func finishedWithAuth(auth: GTMOAuth2Authentication!, error: NSError!) {
        print("received auth \(auth), error \(error)")

        if (GPPSignIn.sharedInstance().userID != nil) {
            let user = GPPSignIn.sharedInstance().googlePlusUser
            println("user name: " + user.name.JSONString() + "\nemail: ")
            if (user.emails != nil){
                print(user.emails.first?.JSONString() ?? "no email")
            } else {
                print("no email")
            }
        } else {
            println("User ID is nil")
        }
    }
}

除此之外,我还有app委托功能(但请注意,某些可选说明符不同)

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    return GPPURLHandler.handleURL(url, sourceApplication:sourceApplication, annotation: annotation)
}
点赞