WKWebView XMLHttpRequest无法加载资源

问题描述 投票:0回答:2

大家!

我有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
2个回答
1
投票

我有一个类似的问题,并用webview配置修复它。只需在viewDidLoad()或类似代码中使用以下代码(如果您使用的是Swift):

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

0
投票

据@Dion回答,对于Obj C.

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
[configuration.preferences setValue:@true forKey:@"allowFileAccessFromFileURLs"];
self.wkWebView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
© www.soinside.com 2019 - 2024. All rights reserved.