javascript 没有互联网访问时,WKWebView 不会完成/超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28971088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WKWebView does not finish/timeout when there is no internet access
提问by depthfirstdesigner
There is a remote Javscript asset tag on a local HTML page I'm navigating to via WkWebView that never finishes downloading (in both the case that the iOS device is not connected to the internet, or the device's internet is too slow).
在我通过 WkWebView 导航到的本地 HTML 页面上有一个远程 Javscript 资产标签,该标签从未完成下载(在两种情况下,iOS 设备未连接到互联网,或者设备的互联网速度太慢)。
Unfortunately, the WKWebView never aborts trying to download this asset nor does it ever move on to my WKNavigationDelegate (i.e. neither -webView:didFailNavigation:withError:
nor -webView:didFinishNavigation: neither
are called), so users are stuck with a blank HTML page that never fully resolves and never errors out.
不幸的是,WKWebView 永远不会中止尝试下载此资产,也不会转移到我的 WKNavigationDelegate(即既不调用-webView:didFailNavigation:withError:
也不-webView:didFinishNavigation: neither
调用),因此用户被困在一个空白的 HTML 页面上,该页面永远不会完全解析,也永远不会出错。
Thankfully in some scenarios I've debugged via the Safari remote iOS Javascript debugger, I've noticed we get the error Failed to load resource: The Internet connection appears to be offline
logged to the console, but in others (like certain devices in Airplane Mode) the WKWebView never seems to timeout with this error or give up, so no WKNavigationDelegate callbacks get fired.
幸运的是,在某些情况下,我通过 Safari 远程 iOS Javascript 调试器进行了调试,我注意到我们将错误Failed to load resource: The Internet connection appears to be offline
记录到控制台,但在其他情况下(例如某些处于飞行模式的设备),WKWebView 似乎永远不会因此错误而超时,或者放弃,所以不会触发 WKNavigationDelegate 回调。
You can reproduce this scenario on your own by using the iOS 8.2 simulator with any WKWebView iOS application, navigate to any locally served HTML file, then add a script tag with any remote URL, then throttle your Internet connection to "100% Loss" using Apple's Network Link Conditioner.
您可以通过将 iOS 8.2 模拟器与任何 WKWebView iOS 应用程序一起使用,导航到任何本地提供的 HTML 文件,然后添加带有任何远程 URL 的脚本标记,然后使用以下命令将 Internet 连接限制为“100% 丢失”,从而自行重现此场景苹果的网络链接调节器。
What would be the best way to force a WkWebView?to timeout/return via an error or success callback to its WKNavigationDelegate?
强制 WkWebView 超时/通过错误或成功回调到其 WKNavigationDelegate 的最佳方法是什么?
回答by Gabriel.Massana
Try:
尝试:
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
You can check the error, usually you will have an error.code -1009:
你可以检查错误,通常你会有一个error.code -1009:
Check this list: Error Codes
检查此列表:错误代码
回答by Stefan Arentz
I think you will have to do this in your HTML page. You can include a small JavaScript there that runs a timer and checks if your remote script has loaded or not.
我认为您必须在 HTML 页面中执行此操作。您可以在其中包含一个小的 JavaScript,它会运行一个计时器并检查您的远程脚本是否已加载。
If it has not loaded then the JavaScript can show an error in the page directly or notify your application through a script message handler.
如果它尚未加载,则 JavaScript 可以直接在页面中显示错误或通过脚本消息处理程序通知您的应用程序。
(I don't think HTML has an API that a page can use to find out about content loading errors. There are some hints about that on Verify External Script Is Loaded)
(我不认为 HTML 有一个 API 页面可以用来找出内容加载错误。在验证外部脚本加载上有一些提示)