Handle and test the WebView process getting terminated
Introduction
This article explains how to handle unexpected termination of the webview process, which is a separate process from the main app process. This separation means that even if your app is stable, the webview process may still crash independently due to issues within the webview.
Such termination can occur for a variety of reasons, including large media resources that does not fit in memory, issues in the scripted content of the publication, or due to system actions like reclaiming resources under memory pressure. It's important to design your app to detect and recover from this situation gracefully.
We will also cover how you can deliberately trigger a webview termination event to test your app’s handling logic.
When this event occurs, the ColibrioReadingSystemView
will no longer function and must be replaced with a new instance.
Be aware that simply recreating the view and reloading the same publication may not be sufficient if the publication itself is the source of the problem. The webview process may crash again, potentially causing an endless crash-reload cycle. To help mitigate this, we provide examples in our sample apps demonstrating how to detect and break out of such loops.
Android
To get notified about the webview process being terminated, use the method ColibrioReadingSystemView.setOnRendererProcessCrash
.
colibrioReadingSystemView.setOnRendererProcessCrash {
// Destroy the ColibrioReadingSystemView to clean up resources
colibrioReadingSystemView.destroy()
// Add your code to replace the ColibrioReadingSystemView and reload the publication
}
As a way to test your logic, use the following function which will terminate the webview process:
fun killWebView() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val webViewRenderProcess = colibrioReadingSystemView.webView?.webViewRenderProcess
webViewRenderProcess?.terminate()
}
}
iOS
To get notified about the webview process being terminated, use the method ColibrioReadingSystemView.setOnWebViewContentProcessDidTerminateListener(_:)
.
class OnWebViewContentProcessDidTerminateListener: OnWebViewContentProcessDidTerminateListener {
func onWebViewContentProcessDidTerminate(_ webView: WKWebView) {
// Add your code to replace the ColibrioReadingSystemView and reload the publication
}
}
As a way to test your logic, follow these instructions to terminate the webview process:
Launch your app in the simulator
Launch the Activity Monitor app on your Mac
Stop the process
com.apple.WebKit.WebContent