iOS 谷歌登录失败

标签 ios swift google-signin

我在 Doc 上做了任何事情,但是当我调用 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
}

在某些 View Controller 中:

GIDSignIn.sharedInstance().signIn()

而且我已经配置了类似 com.googleusercontent.apps.598xxx...xxx 的 URL 方案。

崩溃截图:

enter image description here

调试区什么也没有显示... :(

最佳答案

你把delegate放在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 Delegate 放入发生登录操作的 YourViewController 中:ư

    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
        }
    }
}

关于iOS 谷歌登录失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44652429/

相关文章:

ios - 添加 CLLocationDistances

ios - NSUserDefaults - 保存和检索 "High Scores"

java - 如何使用 Google Sign in 授权 Youtube Data api 请求?

javascript - 移动设备上的网站滚动 (IOS) JS

ios - 如何将 TabBarController 与导航 Controller 结合使用?

ios - 未以编程方式检测 Dismiss Popover

ios - 在 uitableview 中过滤后执行 segue

reactjs - 请求 Google 日历权限时弹出额外补助金

android - statusCode=DEVELOPER_ERROR 在谷歌登录

android - 无法在我的 Mac 上设置 IBM Worklight