【问题标题】:How to determine which URL scheme to use in `application(_ app: UIApplication, open url: URL`如何确定在 `application(_ app: UIApplication, open url: URL` 中使用哪个 URL 方案
【发布时间】:2021-10-22 11:30:39
【问题描述】:

我将 Google Drive SDK 集成到我的应用中。现在我也想集成 Dropbox,在功能中可能会有更多其他云驱动器。由于现在有两个 URL 方案,我如何确定登录发生时 AppDelegate 方法 func application(_ app: UIApplication, open url: 使用的是哪个 URL 方案?

因为我在 info.plist 中为每个 URL 方案提供了一个标识符。我想也许我可以使用url.scheme.identifer 来区分彼此,但是尝试失败了。他们没有那个链属性。

或者我可以在用户按下登录按钮时手动设置一个标志。那么正确的做法是什么呢?

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        
        // Google SignIn
        return GIDSignIn.sharedInstance.handle(url)
        
        // Dropbox SignIn
        return DropboxClientsManager.handleRedirectURL(url) { authResult in
            guard let authResult = authResult else { return }
            switch authResult {
            case .success(let token):
                print("Success! User is logged into Dropbox with token: \(token)")
            case .cancel:
                print("User canceld OAuth flow.")
            case .error(let error, let description):
                print("Error \(error): \(String(describing: description))")
            }
        }
    }

【问题讨论】:

  • 查看 google 文档中的 #2:developers.google.com/identity/sign-in/ios/sign-in,其中显示“// 处理其他自定义 URL 类型。”。我认为这基本上就是您返回 Dropbox 句柄的地方
  • 谢谢,这是确定它是否来自谷歌登录的正确方法。但是我仍然需要一种方法来检查 URL 方案来自哪个,因为计划中会有更多的云驱动器,例如 One Drive、Mega。

标签: ios swift url-scheme


【解决方案1】:

试试这个

if GIDSignIn.sharedInstance()?.handle(url) == true {
      return true
} else {
      return DropboxClientsManager.handleRedirectURL(url) { authResult in
             guard let authResult = authResult else { return }
             switch authResult {
             case .success(let token):
                 print("Success! User is logged into Dropbox with token: \(token)")
             case .cancel:
                 print("User canceld OAuth flow.")
             case .error(let error, let description):
                 print("Error \(error): \(String(describing: description))")
             }
       }
 }

【讨论】:

    【解决方案2】:

    好的,经过几次测试。现在我在依赖项中添加了一个标志,然后每次用户按下登录按钮时,都会相应地设置标志,如google drivedropbox

    当 AppDelegate 或 SceneDelegate 中的open url 方法被触发时。它将首先检查此标志以打开 url 方案。目前它运行良好,但我不确定这是否是最佳做法。

        func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
            guard let urlFlag = MyDependency.urlFlag else { return false }
            
            if urlFlag == .googleDrive {
                // Google SignIn
                return GIDSignIn.sharedInstance.handle(url)
            } else if {
                ///
            } else if {
                ///
            }
            
            return false
        }
    

    【讨论】:

      猜你喜欢
      • 2016-11-22
      • 2018-03-23
      • 2016-04-17
      • 1970-01-01
      • 2016-10-01
      • 2013-11-05
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多