Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I terminate a WKWebView to trigger webViewWebContentProcessDidTerminate?

Tags:

ios

wkwebview

I want to verify how my app handles webViewWebContentProcessDidTerminate.

I can call my delegate's webViewWebContentProcessDidTerminate directly, but this does not give me full confidence that it behaves as it would in real life.

How can I terminate a WKWebView, causing this callback to be triggered?

like image 798
Henrik N Avatar asked Oct 16 '25 06:10

Henrik N


1 Answers

If you may know webViewWebContentProcessDidTerminate will be called when com.apple.WebKit.WebContent will be terminated by operating system.

In most cases it will be caused by Out Of Memory issues. Personally I have been facing when WkWebView used a lot of memory but not at not critical level to terminate at the same time another view has been placed over Webview (it means that com.apple.WebKit.WebContent goes to background).

If you are interested how to realistically trigger calling this method, try to crash it by OOM error:

  1. load a HTML page with next JS string in the body:

    const OOMString = Array(1024 * 1024 * 1000).fill('A').join(''));

  2. if you are going to do so from your native side (ObjectiveC/Swift), here is another example:

    [wkWebView evaluateJavaScript:@"window.OOMString = Array(1024 * 1024 * 1000).fill('A').join(''));" completionHandler:nil];

You need to take into account that webViewWebContentProcessDidTerminate will be called by operating system decides to terminate your process. For different devices will be different size of memory allowed to occupy. I'm highly not recommend you to load memory this way because it can impact your user experience or clients experience. the best approach here is to do as much as you can to do not allow system to call this method.

like image 122
esperant Avatar answered Oct 17 '25 19:10

esperant