【问题标题】:How to inject/evaluate JS in multiple pages of an iOS webview?如何在 iOS webview 的多个页面中注入/评估 JS?
【发布时间】:2020-02-07 15:14:13
【问题描述】:

好像android的WebViewClient有这样的方法:onProgressChanged()

ios 的 wkwebview 有什么用吗? (或任何其他 iOS 网络视图)

Webview 的 onNavigationStateChange在这里似乎没有帮助。

想在www.example.comwww.example.com/about 上评估 JS。

【问题讨论】:

    标签: ios swift iphone webview wkwebview


    【解决方案1】:

    onNavigationStateChange 到底是什么意思?这对我来说听起来并不熟悉。

    您应该实现 WKNavigationDelegate。 [https://developer.apple.com/documentation/webkit/wknavigationdelegate]

    如果你想在加载一个url后加载JS,你可以实现webView(_, didFinish: WKNavigation)。 WKNavigation 提供 api 来检查请求/url。
    要在导航完成之前加载一些 JS,您可以尝试使用 webView(WKWebView, decidePolicyFor: WKNavigationAction) [https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455641-webview]
    webView(WKWebView, decidePolicyFor: WKNavigationResponse) [https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455643-webview]

    【讨论】:

      【解决方案2】:

      请参阅专门为此目的设计的WKUserScript。在创建 WKWebView 时,会添加一个 WKUserContentController,它可以使用addUserScript 添加脚本。

      【讨论】:

      • 我尝试添加多个 userScript 但不起作用,它是替换 userScript,而不是添加更多
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 2020-08-12
      • 2015-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多