WKWebView process terminates without sandbox entitlement

Viewed 1277

I'm trying to build a simple MacOS app with a web view. I'm using WKWebView and for no obvious reason the webViewWebContentProcessDidTerminate() method is called immediately after the web view is initialized without the web view even trying to load anything. After hours of looking in the documentation and in issues online, I cannot figure out why this is happening and how to prevent it. The only thing suggested online was to reload the web view when this happens which results in the method getting called again a moment later and so on endlessly. I'm now down to a completely empty MacOS app with the following source code:

class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!
    var webView: WKWebView!

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
        webView.navigationDelegate = self
    }
}

extension AppDelegate: WKNavigationDelegate {
    func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
        print("web view process did terminate")
        webView.reload()
    }
}

It doesn't matter if the web view is in the view hierarchy, has a non-zero frame and generally if it is visible or not. I've also enabled arbitrary loads just for the sake of trying out everything.

I would really appreciate some help on this. I'm compiling against 10.13 SDK and running on a MacOS 10.12.

Thanks.

2 Answers

I managed to figure it out. For anybody else struggling with this, you need to add the com.apple.security.network.client entitlement to your app.

If wkwebview is getting terminated, you can add reload method inside webViewWebContentProcessDidTerminate method, which basically reloads the webview.

Check the below code, file name: CRAWKWebView.m

- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView
{
  RCTLogWarn(@"Webview Process Terminated");
  if (webView) {
    [webView reload];
  }
}

reload instance is provided by wkwebview.

Related