【问题标题】:Deep link URL Scheme doesn't call any function(swift)深层链接 URL 方案不调用任何函数(swift)
【发布时间】:2019-10-02 11:01:43
【问题描述】:

我实现深层链接的是 iOS。我已经在 Project-Setting->Info->Url type 中配置了 URL Scheme URL 方案:洗车角色:查看者

当我输入 carwash://something 时,浏览器要求打开应用程序,但在我处理应该发生什么操作的应用程序中没有调用任何内容。

苹果文档说您应该在 AppDelegate 中覆盖应用程序(打开 url),但深层链接会调用它并且应用程序会以最后状态打开

application:openURL:options:' 没有被调用

这是我的代码,无法正常工作

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

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        if let url = launchOptions?[UIApplication.LaunchOptionsKey.url] as? URL {
           /// some
            fatalError()
        }
    GMSServices.provideAPIKey("")

    return true
} 

迅速 5 模拟器:iOS 13

【问题讨论】:

标签: ios swift deep-linking url-scheme


【解决方案1】:

经过几天与方案网址的斗争,我终于找到了答案。 我的模拟器的 iOS 版本是 13 。在 iOS 13 UIApplication 中,打开的 url 没有被调用(至少在我的情况下可能)

您应该覆盖 iOS 13 中可用的 SceneDelegate 中的以下函数:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    if let url = URLContexts.first?.url{
        print(url)

    }
}

编辑: 如果用户在您的应用未运行时点击该链接,您将不会响应它

你必须实现 scene(_:willConnectTo:options:) 来检测 options: 中的 URLContext 并处理它。

【讨论】:

  • 对了一半。如果 URL 在您的应用未运行时到达,这将不起作用。
  • 那你有什么建议?
  • 这不是我“建议”的问题,这是事实。您必须实现scene(_:willConnectTo:options:) 来检测options: 中的URLContext 并进行处理。否则,如果用户在您的应用未运行时点击该链接,您将不会对其做出响应。试试看,你会看到的。
  • 谢谢水晶球亚光。非常有用的评论。
【解决方案2】:

在 iOS 13+ 或场景委托中,有两个方法会调用,willPerformHTTPRedirection 方法肯定会调用。

  func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        if userActivity.activityType == NSUserActivityTypeBrowsingWeb, let url =      userActivity.webpageURL {
          
        }
      }
    }

 func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Swift.Void) {
            if let url = request.url {
               guard let detailDictionary = String.queryParameters(from: url) else { return }

            }
        }
    }

通过这个函数解析url:

static func queryParameters(from url: URL) -> [String: String]? {
    let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
    var queryParams: [String : String]? = nil
    if let components = urlComponents?.queryItems {
      queryParams = [String: String]()
      for queryItem in components {
        if queryItem.value == nil {
          continue
        }
        queryParams?[queryItem.name] = queryItem.value
      }
    }
    return queryParams
  }

【讨论】:

  • queryParameters 对我新版本的 firebase 有帮助
【解决方案3】:

您应该在 Info.plist 文件中配置您的深层链接

例子:

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
            <key>CFBundleURLName</key>
            <string>carwash</string> // your identifier
            <key>CFBundleURLSchemes</key>
            <array>
                <string>carwash</string> // schema
            </array>
        </dict>
    </array>

【讨论】:

  • 它已经声明了。我可以使用深层链接打开应用程序,但我无法处理它
【解决方案4】:

你应该使用application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -&gt; Bool

请注意,您的方法中没有 return 语句

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

模拟器中的链接最好通过终端完成

xcrun simctl openurl booted “carwasher://deeplink”

【讨论】:

  • 感谢您的回答。我复制了您的代码,但应用程序通过深层链接打开,但没有调用该函数,也不会打印任何内容
  • 尝试将信息选项卡中的角色从查看者更改为编辑者
【解决方案5】:

对于 iOS13+ 中的我来说,以下工作(在我的 SceneDelegate 中):

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Set up everything you need
    // ...
    
    // Handle deep link on cold start
    if let url = connectionOptions.userActivities.first?.webpageURL {
        handle(url: url)
    }
}

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    if let url = userActivity.webpageURL {
        // Handle deep link when the app is running
        handle(url: url)
    }
}

【讨论】:

    猜你喜欢
    • 2016-06-02
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多