Swift Google+ 登录 GPPSignIn.sharedInstance().userID 始终等于 nil
Swift Google+ login GPPSignIn.sharedInstance().userID equals always nil
我正在尝试使用 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")
}
在我点击 "Login" 按钮后,Safari 选项卡打开,我可以输入我的 Google 电子邮件和密码,它会询问某些事情的权限,然后在按下接受按钮后它 returns 回到应用程序。我的 userEmailLable 没有改变,它打印 "User ID is nil" 作为输出。它一直在发生,没有一次成功登录。
我的 Google 框架都很好,URL 也正确,在 Google 开发者控制台中一切正常。
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")
}
}
}
除此之外,我还有应用程序委托功能(但请注意,一些可选说明符不同)
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
return GPPURLHandler.handleURL(url, sourceApplication:sourceApplication, annotation: annotation)
}
我正在尝试使用 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")
}
在我点击 "Login" 按钮后,Safari 选项卡打开,我可以输入我的 Google 电子邮件和密码,它会询问某些事情的权限,然后在按下接受按钮后它 returns 回到应用程序。我的 userEmailLable 没有改变,它打印 "User ID is nil" 作为输出。它一直在发生,没有一次成功登录。
我的 Google 框架都很好,URL 也正确,在 Google 开发者控制台中一切正常。
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")
}
}
}
除此之外,我还有应用程序委托功能(但请注意,一些可选说明符不同)
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
return GPPURLHandler.handleURL(url, sourceApplication:sourceApplication, annotation: annotation)
}