【问题标题】:WKWebView XMLHttpRequest Failed to load resourceWKWebView XMLHttpRequest 加载资源失败
【发布时间】:2018-07-25 01:42:07
【问题描述】:

大家!

我有 WKWebView,它在里面加载带有 js 的本地 XML。

function loadSegment(url,uuid) {
    let xhr = new XMLHttpRequest();
    xhr.onerror = () => rejected();
    xhr.onabort = () => rejected();
    xhr.ontimeout = () => rejected();
    xhr.onload = () => xhr.status === 200 ? handleSegment(url, uuid, xhr.response) : rejected();
    xhr.open("GET", url);
    xhr.responseType = "arraybuffer";
    xhr.send(null)
}

function handleSegment(url,uuid,res) {
// url, uuid, res to json
// webkit.messageHandlers.LoadHandler.postMessage(<#JSON#>)
}

function rejected() {

}

WebView 成功加载文件后我评估

webView.evaluateJavaScript(script, completionHandler: { (response, error) in
// Error handling
}

脚本评估成功。但在 Safari Inspector 中,我看到以下错误

[Error] Failed to load resource: The network connection was lost. (05.ts, line 0)
[Error] Failed to load resource: A server with the specified hostname could not be found. (05.ts, line 0)

它尝试执行以下 url 的请求:http://edge.flowplayer.org/cilla_black_bean_sauce/1/216p-lo/00.ts

最奇怪的是它在模拟器上完美运行。

有没有人遇到过类似的问题?

【问题讨论】:

标签: javascript ios swift xmlhttprequest wkwebview


【解决方案1】:

我遇到了类似的问题,并使用 webview-configuration 修复了它。只需在您的viewDidLoad() 或类似代码中使用以下代码(如果您使用的是 Swift):

webView.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")

【讨论】:

    【解决方案2】:

    根据@Dion 的回答,对于 Obj C

    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    [configuration.preferences setValue:@true forKey:@"allowFileAccessFromFileURLs"];
    self.wkWebView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-24
      • 2018-01-15
      • 2013-02-26
      • 2019-05-28
      • 2016-06-24
      • 2016-07-01
      • 2011-06-21
      相关资源
      最近更新 更多