When A WebViewController
push B WebViewController
, the settimeout
function in A WebViewController
will be stop working until returning to A WebViewController
.
On a PC, switching tabs in a WebView does not affect the execution of the setTimeout function, but on iOS, this issue occurs.
Is there a way to keep the setTimeout function running even when the WebView is inactive?
Is there any official documentation or code explanation for this issue?
Demo Code
class DemoVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .lightGray
}
}
class ViewController: UIViewController {
private lazy var webView = { () -> WKWebView in
let webViewConfiguration = WKWebViewConfiguration()
let userContentController = WKUserContentController()
userContentController.add(self, name: "logHandler")
webViewConfiguration.userContentController = userContentController
let webView = WKWebView(frame: .zero, configuration: webViewConfiguration)
let script = """
(function() {
var oldLog = console.log;
console.log = function(message) {
oldLog(message);
window.webkit.messageHandlers.logHandler.postMessage(message);
};
})();
"""
webView.configuration.userContentController.addUserScript(WKUserScript(source: script, injectionTime: .atDocumentStart, forMainFrameOnly: false))
let htmlString = """
<!DOCTYPE html>
<html>
<head>
<title>WKWebView Test</title>
</head>
<body>
<h1>Testing JavaScript in WKWebView</h1>
<script>
setInterval(function() {
console.log("1");
}, 3000);
</script>
</body>
</html>
"""
webView.loadHTMLString(htmlString, baseURL: nil)
return webView
}()
private lazy var button = { () -> UIButton in
let button = UIButton(frame: .zero)
button.setTitle("push", for: .normal)
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(pushAction), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
webView.frame = view.bounds
view.addSubview(webView)
view.addSubview(button)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
button.center = webView.center
}
@objc private func pushAction() {
navigationController?.pushViewController(DemoVC(), animated: true)
}
}
extension ViewController: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "logHandler", let log = message.body as? String {
print("JavaScript log: (log)")
}
}
}
2