diff --git a/Sources/Web/CrossmintWebView.swift b/Sources/Web/CrossmintWebView.swift index 509c107..29868ee 100644 --- a/Sources/Web/CrossmintWebView.swift +++ b/Sources/Web/CrossmintWebView.swift @@ -44,6 +44,12 @@ public struct CrossmintWebView: UIViewRepresentable { } public func makeUIView(context: Context) -> WKWebView { + if let existingWebView = webViewCommunicationProxy.webView { + webViewCommunicationProxy.onWebViewMessage = onWebViewMessage + webViewCommunicationProxy.onUnknownMessage = onUnknownMessage + return existingWebView + } + let configuration = WKWebViewConfiguration() let userContentController = WKUserContentController() @@ -74,5 +80,7 @@ public struct CrossmintWebView: UIViewRepresentable { } public func updateUIView(_ webView: WKWebView, context: Context) { + webViewCommunicationProxy.onWebViewMessage = onWebViewMessage + webViewCommunicationProxy.onUnknownMessage = onUnknownMessage } } diff --git a/Sources/Web/WebViewCommunicationProxy.swift b/Sources/Web/WebViewCommunicationProxy.swift index 5608e82..b12df7f 100644 --- a/Sources/Web/WebViewCommunicationProxy.swift +++ b/Sources/Web/WebViewCommunicationProxy.swift @@ -32,7 +32,8 @@ extension WebViewCommunicationProxy { public class DefaultWebViewCommunicationProxy: NSObject, ObservableObject, WKScriptMessageHandler, WebViewCommunicationProxy { public let name = "crossmintMessageHandler" - public weak var webView: WKWebView? + public var webView: WKWebView? + public var onWebViewMessage: (any WebViewMessage) -> Void = { _ in } public var onUnknownMessage: (String, Data) -> Void = { _, _ in } @@ -48,6 +49,12 @@ public class DefaultWebViewCommunicationProxy: NSObject, ObservableObject, WKScr } } + deinit { + webView?.navigationDelegate = nil + webView?.configuration.userContentController.removeScriptMessageHandler(forName: name) + webView = nil + } + public func loadURL(_ url: URL) async throws { guard let webView = webView else {