Google 签名 (GIDSignIn) 在 iOS 10.3 之前可以完美运行,但在 iOS 11 中则不行。
在 iOS 11 中:
- (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController
方法仅在第一次调用。随后的登录尝试(在注销或取消登录之后)不会调用上述回调。调用 GIDSignIn.signin 方法什么也不做。
Google 签名 (GIDSignIn) 在 iOS 10.3 之前可以完美运行,但在 iOS 11 中则不行。
在 iOS 11 中:
- (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController
方法仅在第一次调用。随后的登录尝试(在注销或取消登录之后)不会调用上述回调。调用 GIDSignIn.signin 方法什么也不做。
就在两天前只解决了这个问题。请找到以下链接,这些链接可能会帮助您了解需要什么。最重要的是您的视图控制器,您从中执行打开 GSignIn 的操作,VC 必须位于 iOS 10 及更高版本的 UINavigationStack 中。 链接: Google 登录在 iOS 中显示黑屏, GIDSignIn 在 iOS 9 中显示白屏
基本上,您不会将两个代表调用到您的 Viewcontroller.Swift 文件中
Xcode 9.2 斯威夫特 4
第 1 步:安装两个 pod
pod 'Google'
pod 'Google/SignIn'
第 2 步:通过提供项目的包标识符来启用 Google Sign。生成配置文件并将其导入您的项目。
第 3 步:转到您的 AppDelegate.swift 并导入两个库。
import GoogleSignIn
import Google
第 4 步:用此代码替换您的 appDelegate 函数。客户端 ID 显示在您导入到项目中的 Google GoogleService-Info.plist 中。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
GIDSignIn.sharedInstance().clientID = "CLIENT_ID"
return true
}
在您的 AppDelegate 文件中实现此委托。GIDSignInDelegate
class AppDelegate: UIResponder, UIApplicationDelegate,GIDSignInDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
print("\(error.localizedDescription)")
} else {
// 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
}
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
let googleDidHandle = GIDSignIn.sharedInstance().handle(url,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplicationOpenURLOptionsKey.annotation])
return googleDidHandle
}
}
第5步:转到ViewController.Swift文件粘贴代码并拖动按钮并与代码中可用的按钮动作连接。您只需将按钮与代码连接
import UIKit
import GoogleSignIn
import Google
class ViewController: UIViewController,GIDSignInUIDelegate, GIDSignInDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if (error == nil) {
// Perform any operations on signed in user here.
print(user.userID) // For client-side use only!
print(user.authentication.idToken) // Safe to send to the server
print(user.profile.name)
print(user.profile.givenName)
print(user.profile.familyName)
print(user.profile.email)
print(user.authentication.accessToken)
print(user.profile)
} else {
print("\(error.localizedDescription)")
}
}
func sign(_ signIn: GIDSignIn!, didDisconnectWith user:GIDGoogleUser!,
withError error: Error!) {
}
@IBAction func onBtnTapGmailLogin(sender: GIDSignInButton) {
GIDSignIn.sharedInstance().signIn()
}
override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().delegate = self
var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}