Sahil Dhiman
Sahil Dhiman

Reputation: 311

Google SignIn not working for iOS 10

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.

Upvotes: 12

Views: 2903

Answers (5)

wormlxd
wormlxd

Reputation: 514

- (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.

Upvotes: 0

Edoardo Vicoli
Edoardo Vicoli

Reputation: 237

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

Upvotes: 2

Sahil Dhiman
Sahil Dhiman

Reputation: 311

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

Upvotes: 0

sRoy
sRoy

Reputation: 267

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

Upvotes: 9

Yifan
Yifan

Reputation: 1245

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

Upvotes: 5

Related Questions