Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Google sign in failed

I have done anything on Doc, but it crashed when I call GIDSignIn.sharedInstance().signIn().

Code:

At AppDelegate:

func configGoogleServer() {
    var configureError: NSError?
    GGLContext.sharedInstance().configureWithError(&configureError)
    assert(configureError == nil, "Error configuring Google services: \(configureError as Optional)")
    GIDSignIn.sharedInstance().delegate = self
}

At some viewController:

GIDSignIn.sharedInstance().signIn()

And I have configured URL scheme like com.googleusercontent.apps.598xxx...xxx.

Crash screenshot:

enter image description here

And there was nothing showing on Debug Area... :(

like image 262
Leo Avatar asked Nov 20 '25 05:11

Leo


1 Answers

You put the delegate inside the AppDelegate.swift, it's not true, you AppDelegate should be like this:

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
        }

And then put the SignIn Delegate inside YourViewController which signing-in action happens:ư

    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
        }
    }
}
like image 176
ngbaanh Avatar answered Nov 22 '25 22:11

ngbaanh