WKWebView 之内存泄漏

2018-09-04  本文已影响0人  izsm

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")
}
上一篇下一篇

猜你喜欢

热点阅读