iOS Google登录失败
我在上做了任何操作,但当我调用iOS Google登录失败,ios,swift,google-signin,Ios,Swift,Google Signin,我在上做了任何操作,但当我调用GIDSignIn.sharedInstance().signIn()时,它崩溃了 代码: 在AppDelegate: func configGoogleServer() { var configureError: NSError? GGLContext.sharedInstance().configureWithError(&configureError) assert(configureError == nil, "Error c
GIDSignIn.sharedInstance().signIn()
时,它崩溃了
代码:
在AppDelegate:
func configGoogleServer() {
var configureError: NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError as Optional)")
GIDSignIn.sharedInstance().delegate = self
}
在某些viewController上:
GIDSignIn.sharedInstance().signIn()
class ViewController: UIViewController,GIDSignInUIDelegate {
override func viewWillAppear(_ animated: Bool) {
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().uiDelegate = self
}
}
我已经配置了URL方案,比如com.googleusercontent.apps.598xxx…xxx
碰撞屏幕截图:
调试区域没有显示任何内容…:(在ViewController中添加以下代码:
GIDSignIn.sharedInstance().signIn()
class ViewController: UIViewController,GIDSignInUIDelegate {
override func viewWillAppear(_ animated: Bool) {
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().uiDelegate = self
}
}
您忘记执行委托:
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
print(error.localizedDescription)
return
}
else{
//handle response
}
}
您将代理放在
AppDelegate.swift
中,这不是真的,您的AppDelegate应该是这样的:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// .. something else
// GOOGLE
// Initialize sign-in
var configureError: NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
// No delegate here
return true
}
func application(_ application: UIApplication,
open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return GIDSignIn.sharedInstance().handle(url,
sourceApplication: sourceApplication,
annotation: annotation)
}
@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if let sourceApplication = options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String {
let annotation = options[UIApplicationOpenURLOptionsKey.annotation]
return GIDSignIn.sharedInstance().handle(url,
sourceApplication: sourceApplication,
annotation: annotation)
}
return true
}
然后将SignIn委托放在您的ViewController中进行签名操作:ư
class YourViewController: UIViewController {
// something else....
func doSignIn() {
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().scopes = YOUR_GOOGLE_SCOPES
if GIDSignIn.sharedInstance().hasAuthInKeychain() {
GIDSignIn.sharedInstance().signInSilently()
} else {
GIDSignIn.sharedInstance().signIn()
}
}
}
extension YourViewController: GIDSignInDelegate, GIDSignInUIDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
withError error: Error!) {
if let error = error {
self.showMessage("Authentication Error", type: .error)
self.service.authorizer = nil
} else {
self.service.authorizer = user.authentication.fetcherAuthorizer()
// PUT YOUR METHOD AFTER SIGNED-IN HERE
}
}
}
你是如何安装sdk的?你可以找到google的示例代码添加抛出的确切错误。是的,你是对的,我应该将ViewController设置为委派,现在它已修复,谢谢!