Skip to content

Commit

Permalink
Use a weak reference to the webview in JustBridge to avoid memory lea…
Browse files Browse the repository at this point in the history
…ks (#30)

* Use a weak reference to the webview in JustBridge to avoid memory leaks

* Make var private
  • Loading branch information
mroloux authored Aug 19, 2024
1 parent bcf0a1f commit 453f340
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions seatsio-ios/JustBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ public class JustBridge: NSObject {
/// unique id for callback
fileprivate var callbackId = 0

fileprivate var webview: WKWebView
private weak var webview: WKWebView?

public init(with webView: WKWebView) {
self.webview = webView
super.init()

self.webview.configuration.userContentController.add(self, name: "bridge")
self.webview!.configuration.userContentController.add(self, name: "bridge")
self.injectBridgeJS()
}

Expand Down Expand Up @@ -170,7 +170,7 @@ extension JustBridge {

fileprivate func injectBridgeJS() {
let script = WKUserScript(source: JustBridge.bridge_js, injectionTime: .atDocumentStart, forMainFrameOnly: true)
self.webview.configuration.userContentController.addUserScript(script)
self.webview!.configuration.userContentController.addUserScript(script)
}

fileprivate func postMessage(_ name: String, data: BridgeData, swiftCallbackId: String? = nil, jsCallbackId: String? = nil, error: String? = nil) {
Expand All @@ -189,7 +189,7 @@ extension JustBridge {

if let messageJSONData = try? JSONSerialization.data(withJSONObject: message, options: []),
let messageJSON = String(data: messageJSONData, encoding: .utf8) {
self.webview.evaluateJavaScript("window.bridge.receiveMessage(\(messageJSON));", completionHandler: nil)
self.webview!.evaluateJavaScript("window.bridge.receiveMessage(\(messageJSON));", completionHandler: nil)
}
}

Expand Down

0 comments on commit 453f340

Please sign in to comment.