WKWebView与JS交互时,会用WKUserContentController注入对象
private lazy var webView: WKWebView = {
let config = WKWebViewConfiguration()
config.userContentController.add(self as WKScriptMessageHandler, name: "Request")
let webView = WKWebView(frame: .zero, configuration: config)
webView.load(URLRequest(url: url))
return webView
}()
这里注入的名称为“Request”,如果我们不移除“Request”的话会导致内存泄漏,所以在viewDidDisappear的时候把“Request”移除
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
webView.configuration.userContentController.removeScriptMessageHandler(forName: "Request")
}
网友评论