【问题标题】:URL not being passed to appdelegateURL 未传递给 appdelegate
【发布时间】:2020-01-14 12:26:53
【问题描述】:

我无法使动态链接或通用链接正常工作。我的应用程序不会读取 URL。我从一个新项目开始,将 com.company.AppLoginView 的 URL 方案和以下代码添加到 AppDelegate 中。我在注释中添加了一个 url:com.company.AppLoginView://Register?User=Name。单击链接请求打开应用程序,然后打开该应用程序。但是,似乎没有任何内容传递给应用程序,并且没有调用以下内容。我究竟做错了什么?这是一个完全空的应用程序,应该可以工作。

func application(_ app: UIApplication, open url: URL,
                 options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    if let scheme = url.scheme,
        scheme.localizedCaseInsensitiveCompare("com.myApp") == .orderedSame,
        let view = url.host {

        var parameters: [String: String] = [:]
        URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems?.forEach {
            parameters[$0.name] = $0.value
        }

        print("View:\(view) Params:\(parameters) Scheme:\(scheme)")

//        redirect(to: view, with: parameters)
    }
    return true
}

【问题讨论】:

    标签: swift xcode appdelegate ios-universal-links


    【解决方案1】:

    这是因为目标是 iOS 13,SceneDelegate 接管了 AppDelegate 的一些功能。特别是,open:url 被 SceneDelegate scene:openURLContexts 取代。

    更多:Apple openURLContexts

    我生命中的 3 天我不会回来!

    【讨论】:

      【解决方案2】:

      您应该将 ULR 方案和 URL 标识符添加到 plist。你可以使用这个:

      <key>CFBundleURLTypes</key>
      <array>
          <dict>
              <key>CFBundleTypeRole</key>
              <string>Editor</string>
              <key>CFBundleURLName</key>
              <string>com.company.AppLoginView</string>
              <key>CFBundleURLSchemes</key>
              <array>
                  <string>com.company.AppLoginView</string>
              </array>
          </dict>
      </array>
      

      并通过 AppDelegate 中的 print in 方法进行检查

      func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
          print(url)
          return true
      }
      

      打开 Safari 并输入您的地址

      com.company.AppLoginView://Register?User=Name

      【讨论】:

      猜你喜欢
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      相关资源
      最近更新 更多