Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WKWebView retain cycle in Reader #24028

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
* [*] Fix an issue with Mastodon connection not working [#23981]
* [**] Send feedback from within the experimental editor "more" options menu. [#23980]
* [**] Support accessing the code editor within the experimental editor. [#23979]
* [*] Fix WKWebView retain cycle in Reader [#24028]

25.6
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class WebCommentContentRenderer: NSObject, CommentContentRenderer {
webView.scrollView.showsVerticalScrollIndicator = false
webView.scrollView.backgroundColor = .clear
webView.configuration.allowsInlineMediaPlayback = true
webView.configuration.userContentController.add(self, name: "eventHandler")

// - warning: It retains the handler. It can't be `self`.
webView.configuration.userContentController.add(ReaderWebViewMessageHandler(), name: "eventHandler")
}

func render() -> UIView {
Expand Down Expand Up @@ -112,20 +114,6 @@ extension WebCommentContentRenderer: WKNavigationDelegate {
}
}

// MARK: - WKScriptMessageHandler

extension WebCommentContentRenderer: WKScriptMessageHandler {

func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
guard let body = message.body as? String,
let event = ReaderWebView.EventMessage(rawValue: body)?.analyticEvent else {
return
}
WPAnalytics.track(event)
}

}

// MARK: - Private Methods

private extension WebCommentContentRenderer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class ReaderWebView: WKWebView {
isInspectable = true
}

configuration.userContentController.add(self, name: "eventHandler")
// - warning: It retains the handler. It can't be `self`.
configuration.userContentController.add(ReaderWebViewMessageHandler(), name: "eventHandler")
}

/// Loads a HTML content into the webview and apply styles
Expand Down Expand Up @@ -311,8 +312,7 @@ class ReaderWebView: WKWebView {
}
}

extension ReaderWebView: WKScriptMessageHandler {

final class ReaderWebViewMessageHandler: NSObject, WKScriptMessageHandler {
enum EventMessage: String {
case articleTextHighlighted
case articleTextCopied
Expand Down Expand Up @@ -342,5 +342,4 @@ extension ReaderWebView: WKScriptMessageHandler {
}
WPAnalytics.track(event)
}

}