情况一:由于内存爆增而导致的加载白屏。解决方法:在wkwebview代理中重新加载
//进程终止(内存消耗过大导致白屏)
func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
print("进程被终止")
webView.reload()
}
情况二:后台运行一段时间,返回app,webview变成白屏。解决方法:注册app返回前台通知。通过js注入,判断当前是否是白屏,如果是白屏,则重新加载
//app返回前台
NotificationCenter.default.addObserver(self, selector: #selector(wk_AppDidEnterPlayground), name: UIApplication.didBecomeActiveNotification, object: nil)
//app返回前台
@objc func wk_AppDidEnterPlayground() {
webView.evaluateJavaScript("document.body.innerHTML") { [weak self] (result, error) in
guard let strongSelf = self else {return}
if let innerHTML = result as? String {
if innerHTML.isEmpty {
strongSelf.webView.reload()
}
}
}
}
网友评论