I am using WKWebView in a Swift app built for iOS 8+. I use instances of WKWebView in a variety of views in my app, e.g. in each of the tabs in my tab view controller, the interface is based on WKWebView.
I and my testers have noticed these views sometimes go completely blank, and after researching that issue, it seems that WKWebView can crash, and the blank view is the result. Luckily, it doesn't bring down the app due to the way WKWebView operates, but I also am not clear on how to trap/log information about what caused it to crash (if that is actually what is happening).
How can I determine if/why a WKWebView has crashed?
My current workaround for the issue is that I use KVO (actually, Facebook's KVOController), to monitor the "URL" property of the WKWebView, and if it goes from non-nil to nil, I assume a crash has happened, and I reload the webview:
kvoController?.observe(webView, keyPath: "URL", options: NSKeyValueObservingOptions.New|NSKeyValueObservingOptions.Old) { (areaViewController, webView, change) -> Void in
if change[NSKeyValueChangeNewKey] is NSNull && !(change[NSKeyValueChangeOldKey] is NSNull) {
areaViewController.setup() // reload our webview
}
}
But obviously it would be nice to figure out the root cause of the crash.
Debugging with WKWebViewOpen Safari and press ⌘+, to open Preference, under the Advanced tab, check “Show Develop menu in menu bar”. Not until we have the project build can we use the Safari debugger. The debugger is under Develop → Your simulator or device.
WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.
A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.
Unlike UIWebView, which does not support server authentication challenges, WKWebView does. In practical terms, this means that when using WKWebView, you can enter site credentials for password-protected websites.
This is very nasty WKWebView issue that we also encountered in Firefox for iOS.
See the following bug report https://bugs.webkit.org/show_bug.cgi?id=148685
I have no great solution but I do have two tips:
First, we were also doing the reload when we found out via KVO that webView.URL
turned nil
. However it turns out that it turns nil
on a number of events. For example when a redirect happens and possibly also when a form is submitted. So this is not ideal.
Second, in iOS9 there is a new API to detect when the WebKit content process has died. We have not tried this yet but I think that will be a better trigger to reload the webView.
There is no documentation yet I think, but you can see this in the header files:
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView NS_AVAILABLE(10_11, 9_0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With