Google SignIn not working for iOS 10

2.8k views Asked by At

I have integrated the GoogleSinIn API in my project with Swift 4.0. It is working on iOS 11.0 but when I'm testing the same on iOS 10.0 it is opening the Google login page on the Safari browser or the device and after signing successfully it is opening the Google search page.

  1. When I click the GoogleSignIn button shown below it opens the browser shown in next image.

  2. Then I fill up the credentials.

  3. After the successful signed in, It redirects to the Google page instead of the application page.

5

There are 5 answers

0
Sahil Dhiman On BEST ANSWER

I was using wrong handler in AppDelegate.

Previously I was using:

private func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool

But it should be:

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool
1
Yifan On

check your GIDSignInUIDelegate, don't forget

func sign(_ signIn: GIDSignIn!, present viewController: UIViewController!) {
    self.present(viewController, animated: true, completion: nil)
}

func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!) {
    self.dismiss(animated: true, completion: nil)
}
0
sRoy On

You have to implement this delegate function in your AppDelegate.

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
     return GIDSignIn.sharedInstance().handle(url as URL!, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
0
Edoardo Vicoli On

Google documentation sucks! I'm using iOS 10 and documentation says to add the second method I wrote only if using iOS 8.0 or older. Don't know why. I got success adding these two methods:

// [START openurl]
func application(_ application: UIApplication,
open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
  return GIDSignIn.sharedInstance().handle(url, sourceApplication: sourceApplication, annotation: annotation)
}
// [END openurl]
@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
return GIDSignIn.sharedInstance().handle(url,
  sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
  annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
0
wormlxd On
- (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url
        options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {


    BOOL handleGoogleSignIn = [[GIDSignIn sharedInstance] handleURL:url
                                              sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                                         annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
    if (handleGoogleSignIn) {
        return handleGoogleSignIn;
    }
}

- (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation {

    BOOL handleGoogleSignIn = [[GIDSignIn sharedInstance] handleURL:url
                           sourceApplication:sourceApplication
                                  annotation:annotation];
    if (handleGoogleSignIn) {
        return handleGoogleSignIn;
    }
}

If you use sourceApplication ,you need write GoogleSignIn return both.