Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent WKWebView from being closed when running Cordova plugin

I am using the cordova-plugin-wkwebview-engine plugin to run my html5 app in Cordova. Because of the WKWebView architecture, the webview is loaded in another thread. Now when I open another thread from Cordova (EG. taking a picture using the camera plugin) and iOS issues a memory warning while the camera view is active, the webview gets killed.

Apparently the WKWebView view is handled like background threads and gets stopped. Now when the camera returns, the app is blank (white screen) because the webview is no longer there. In UIWebView (which we abandoned due to better performance in WKWebView) ran on the main thread of the app and therefore never got killed.

Is there any way to prevent the WKWebView from being closed? If the app cannot keep the main app running the WKWebView would be pretty useless for anything other than showing web page overlays.

like image 623
Rob Boerman Avatar asked Dec 22 '15 11:12

Rob Boerman


1 Answers

It looks like there are quite a few issues when it comes to Memory Management for WKWebView still, along with some iOS 9 security changes that may be useful for solving your problem.

HERE is an Apple Support Staff's reasoning as to why your WKWebView is returning blank, instead of just crashing your app:

While its possible that the WKWebView process could exceed its memory budget, doing so would not cause your app to be terminated and should at most result in a blank view.

Also, THIS question sheds light on the NSAppTransportSecurity Protocol (iOS 9 changes) that was also causing a similar problem. Although it's not memory related, it may be worth looking into if you're using a non-secure URL in either WKWebView instance.

If you're using a HTTP URL during either request, you'll have to include:

<key>NSAppTransportSecurity</key>  
<dict> 
   <key>NSAllowsArbitraryLoads</key><true/>  
</dict> 

in your Application's .plist file.

If you're still unable to solve the problem, I would recommend checking out the chromium forum for WKWebView, along with possibly lowering the quality parameter for the camera plugin to reduce the memory issue.

like image 197
ChrisHaze Avatar answered Nov 04 '22 07:11

ChrisHaze