简体   繁体   English

Google SignIn不适用于iOS 10

[英]Google SignIn not working for iOS 10

I have integrated the GoogleSinIn API in my project with Swift 4.0 . 我已经在我的项目中使用Swift 4.0集成了GoogleSinIn API 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. 它正在iOS 11.0但是当我在iOS 10.0上测试它时,它正在Safari浏览器或设备上打开Google登录页面,并且在成功签名后它将打开Google搜索页面。

  1. When I click the GoogleSignIn button shown below it opens the browser shown in next image. 当我点击下面显示的GoogleSignIn按钮时,它会打开下一张图片中显示的浏览器。

  2. Then I fill up the credentials. 然后我填写凭据。

  3. After the successful signed in, It redirects to the Google page instead of the application page. 成功登录后,它会重定向到Google页面而不是应用程序页面。

You have to implement this delegate function in your AppDelegate . 您必须在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])
}

check your GIDSignInUIDelegate , don't forget 检查你的GIDSignInUIDelegate ,别忘了

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

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. 我正在使用iOS 10和文档说添加我写的第二种方法,只有使用iOS 8.0或更早版本。 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])
}

I was using wrong handler in AppDelegate . 我在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
- (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. 如果您使用sourceApplication,则需要编写GoogleSignIn两者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM