在 Swift 3 中处理 Google 登录时遇到问题 [英] Trouble handling Google sign in Swift 3

查看:37
本文介绍了在 Swift 3 中处理 Google 登录时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 iOS 新手,在 Swift 3 中处理应用程序委托 URL 时遇到问题,我真的可以使用一些指针.

I am new to iOS and am having trouble with app delegate URL handling in Swift 3, and I could really use some pointers.

以下代码在 Swift 2.3 中运行良好:

The below code works perfectly fine in Swift 2.3:

func application(application: UIApplication,
  openURL url: NSURL, options: [String: AnyObject]) -> Bool {
    return GIDSignIn.sharedInstance().handleURL(url,
        sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
        annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}


func application(application: UIApplication,
  openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    var options: [String: AnyObject] = [UIApplicationOpenURLOptionsSourceApplicationKey: sourceApplication,
                                        UIApplicationOpenURLOptionsAnnotationKey: annotation]
    return GIDSignIn.sharedInstance().handleURL(url,
        sourceApplication: sourceApplication,
        annotation: annotation)
}

当我直接从 Firebase 网站粘贴 swift 2.3 代码时,Xcode 提示我很多方法已经改变.特别是 options 对象.

When I paste the swift 2.3 code straight from the Firebase website, Xcode prompts me that a lot of the methods have changed. In particular, the options object.

在 Swift 2.3 中,我可以使用 options[UIApplicationOpenURLOptionsSourceApplicationKey] 访问源应用程序和使用 options[UIApplicationOpenURLOptionsAnnotationKey]

In Swift 2.3, I can access the source application using options[UIApplicationOpenURLOptionsSourceApplicationKey] and the annotation using options[UIApplicationOpenURLOptionsAnnotationKey]

Xcode 提示我将其更改为 UIApplicationOpenURLOptionsKey.sourceApplication._rawValue as StringUIApplicationOpenURLOptionsKey.annotation._rawValue as String

Xcode prompts me to change it to UIApplicationOpenURLOptionsKey.sourceApplication._rawValue as String and UIApplicationOpenURLOptionsKey.annotation._rawValue as String

然而,当我进行建议的更改时,Google 会返回一个 URL,但没有任何反应.它没有重定向到应用程序,而是继续进入本地谷歌网站,对我来说:www.google.co.nz

Yet when I make the suggested changes, Google returns a URL, but nothing happens. Instead of redirecting to the app, it proceeds to enter the local google site, for me: www.google.co.nz

我拥有的完整 Swift 3 版本如下所示:

The full Swift 3 version I have looks like this:

func application(application: UIApplication,
                 openURL url: NSURL,
                 options: [String: AnyObject]) -> Bool {
    print(UIApplicationOpenURLOptionsKey.sourceApplication._rawValue as String)
    print(UIApplicationOpenURLOptionsKey.annotation._rawValue as String)

    return GIDSignIn.sharedInstance().handle(url as URL!,
                                             sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication._rawValue as String] as! String!,
                                             annotation: options[UIApplicationOpenURLOptionsKey.annotation._rawValue as String])
}

func application(application: UIApplication,
             openURL url: NSURL,
             sourceApplication: String?,
             annotation: AnyObject?) -> Bool {
    var options: [String: AnyObject] = [UIApplicationOpenURLOptionsKey.sourceApplication._rawValue as String: sourceApplication as AnyObject,
                                        UIApplicationOpenURLOptionsKey.annotation._rawValue as String: annotation!]

    print(UIApplicationOpenURLOptionsKey.sourceApplication._rawValue as String)
    print(UIApplicationOpenURLOptionsKey.annotation._rawValue as String)

    return GIDSignIn.sharedInstance().handle(url as URL!,
                                             sourceApplication: sourceApplication,
                                             annotation: annotation)
}

推荐答案

Xcode 8 Swift 3

如果您将多个 URL Schemes 与 Google Sign In 一起使用,请按如下方式使用:

If you are using multiple URL Schemes along with Google Sign In, use it like this:

func application(_ app: UIApplication,
                 open url: URL,
                 options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

    if(url.scheme!.isEqual("fbXXXXXXXXXXX")) {
        return SDKApplicationDelegate.shared.application(app, open: url, options: options)

    } else {
        return GIDSignIn.sharedInstance().handle(url as URL!,
                                    sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String!,
                                    annotation: options[UIApplicationOpenURLOptionsKey.annotation])
    }
}

这篇关于在 Swift 3 中处理 Google 登录时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆