Use a wrapper class to prevent a circular reference between the web view and the article controller

This commit is contained in:
Maurice Parker 2019-10-16 11:31:20 -05:00
parent acbbee870e
commit 0182fb7296
1 changed files with 16 additions and 1 deletions

View File

@ -112,7 +112,7 @@ class ArticleViewController: UIViewController {
webView.uiDelegate = self
webView.configuration.userContentController.removeScriptMessageHandler(forName: MessageName.imageWasClicked)
webView.configuration.userContentController.add(self, name: MessageName.imageWasClicked)
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasClicked)
// Even though page.html should be loaded into this webview, we have to do it again
// to work around this bug: http://www.openradar.me/22855188
@ -378,6 +378,21 @@ extension ArticleViewController: WKScriptMessageHandler {
}
class WrapperScriptMessageHandler: NSObject, WKScriptMessageHandler {
// We need to wrap a message handler to prevent a circlular reference
private weak var handler: WKScriptMessageHandler?
init(_ handler: WKScriptMessageHandler) {
self.handler = handler
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
handler?.userContentController(userContentController, didReceive: message)
}
}
// MARK: UIViewControllerTransitioningDelegate
extension ArticleViewController: UIViewControllerTransitioningDelegate {