【问题标题】:UIWebView stopped getting JS events in shouldStartLoadWith under Swift3UIWebView 停止在 Swift3 下的 shouldStartLoadWith 中获取 JS 事件
【发布时间】:2016-12-19 19:44:35
【问题描述】:

由于某些 WKWebView 不适合我的原因,我在我的应用程序中使用 UIWebView。 解释一下,我刚刚完成了我的应用程序转换为 Swift3(是 2.2)和我的 shouldStartLoadWith 函数没有收到 JS 事件。 如果我在转换之前运行我以前的构建 - 一切都很完美。

我的代码如下所示:

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {

        let url:URL = request.url!
        if (url.scheme == "something"){

            if (url.host != nil){
                var properString = url.absoluteString.removingPercentEncoding!
                properString = properString.replacingOccurrences(of: "something://", with: "")
                performJSFunction(message: properString)
            }

            return false;
        }

        return true
    }

注意:服务器端/html 页面没有任何变化。

【问题讨论】:

  • 你找到解决办法了吗?
  • 是的,看看我的回答

标签: javascript uiwebview swift3 xcode8 uiwebviewdelegate


【解决方案1】:

经过多次尝试,我发现UIWebView 会在开头收到https://http:// 的事件。

所以如果你以前是这样得到的:

appname://something

现在你应该得到它:

https://appname//something

(注意“appname”后面的点会被自动删除——即使你通过src attribute发送。

我知道这不是你想要的——但它确实很好用。

【讨论】:

  • 我应该如何从本地 HTML 文件中获取 https/http?有什么好办法吗?
  • 如果我理解你的问题,我就是这样写的:如果我得到一个以我选择的 src 属性开头的 url(如我的回答中所述),我不会让它继续这个过程并且如果不是 - 我表现得像一个普通的网址
【解决方案2】:

对于那些尝试做同样工作但使用本地 html 文件的人来说,这与https://http:// 没有任何关系。

解决方案是将<button> 更改为<a> 标记。

<a onclick="interface.callFromJS()" href="inapp://capture" ontouchstart="">Your Button Message</a>

然后你就可以在那里捕捉到这些事件:

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {

        if (request.url?.scheme == "inapp")
        {
            doYourAction()      
        }

        return true

    }

希望对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-03
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    相关资源
    最近更新 更多