From 65ba7a299c8150b2fe91542661991381b04dcc63 Mon Sep 17 00:00:00 2001 From: Andrew Balmer Date: Wed, 18 Dec 2024 11:01:07 -0500 Subject: [PATCH 1/4] added closeHandler.js script --- .../Resources/Scripts/closeHandler.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Sources/KlaviyoUI/KlaviyoWebView/Resources/Scripts/closeHandler.js diff --git a/Sources/KlaviyoUI/KlaviyoWebView/Resources/Scripts/closeHandler.js b/Sources/KlaviyoUI/KlaviyoWebView/Resources/Scripts/closeHandler.js new file mode 100644 index 00000000..2a3a528a --- /dev/null +++ b/Sources/KlaviyoUI/KlaviyoWebView/Resources/Scripts/closeHandler.js @@ -0,0 +1,15 @@ +// +// closeHandler.js +// klaviyo-swift-sdk +// +// Created by Andrew Balmer on 12/18/24. +// + + +window.addEventListener("klaviyoForms", function(e) { + if (e.detail.type == 'close') { + if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.closeHandler) { + window.webkit.messageHandlers.closeHandler.postMessage('close'); + } + } +}); From 3e19b191538fb65aa748416a7d521aa35cf95880 Mon Sep 17 00:00:00 2001 From: Andrew Balmer Date: Wed, 18 Dec 2024 11:02:10 -0500 Subject: [PATCH 2/4] inject closeHandler script into form --- Sources/KlaviyoUI/KlaviyoWebView/KlaviyoWebViewModel.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/KlaviyoUI/KlaviyoWebView/KlaviyoWebViewModel.swift b/Sources/KlaviyoUI/KlaviyoWebView/KlaviyoWebViewModel.swift index 1f3a6e99..110fd765 100644 --- a/Sources/KlaviyoUI/KlaviyoWebView/KlaviyoWebViewModel.swift +++ b/Sources/KlaviyoUI/KlaviyoWebView/KlaviyoWebViewModel.swift @@ -27,7 +27,10 @@ class KlaviyoWebViewModel: KlaviyoWebViewModeling { private static func initializeLoadScripts() -> [String: WKUserScript] { var scripts: [String: WKUserScript] = [:] - // TODO: initialize scripts + if let closeHandlerScript = try? FileIO.getFileContents(path: "closeHandler", type: "js") { + let script = WKUserScript(source: closeHandlerScript, injectionTime: .atDocumentEnd, forMainFrameOnly: true) + scripts["closeHandler"] = script + } return scripts } From 511ca6d6bf644ddc22fc5de7e8afae7fd31f43f1 Mon Sep 17 00:00:00 2001 From: Andrew Balmer Date: Wed, 18 Dec 2024 11:02:26 -0500 Subject: [PATCH 3/4] handle close button tap --- Sources/KlaviyoUI/KlaviyoWebView/KlaviyoWebViewModel.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/KlaviyoUI/KlaviyoWebView/KlaviyoWebViewModel.swift b/Sources/KlaviyoUI/KlaviyoWebView/KlaviyoWebViewModel.swift index 110fd765..963cba92 100644 --- a/Sources/KlaviyoUI/KlaviyoWebView/KlaviyoWebViewModel.swift +++ b/Sources/KlaviyoUI/KlaviyoWebView/KlaviyoWebViewModel.swift @@ -42,6 +42,9 @@ class KlaviyoWebViewModel: KlaviyoWebViewModeling { } func handleScriptMessage(_ message: WKScriptMessage) { - // TODO: handle script message + if message.name == "closeHandler" { + // TODO: handle close button tap + print("user tapped close button") + } } } From 8ba7de17c9ff7f5bd427f99e07a4cc20cd2980e2 Mon Sep 17 00:00:00 2001 From: Andrew Balmer Date: Wed, 18 Dec 2024 11:48:10 -0500 Subject: [PATCH 4/4] use optional chaining --- .../KlaviyoWebView/Resources/Scripts/closeHandler.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sources/KlaviyoUI/KlaviyoWebView/Resources/Scripts/closeHandler.js b/Sources/KlaviyoUI/KlaviyoWebView/Resources/Scripts/closeHandler.js index 2a3a528a..1e4f8ea4 100644 --- a/Sources/KlaviyoUI/KlaviyoWebView/Resources/Scripts/closeHandler.js +++ b/Sources/KlaviyoUI/KlaviyoWebView/Resources/Scripts/closeHandler.js @@ -8,8 +8,6 @@ window.addEventListener("klaviyoForms", function(e) { if (e.detail.type == 'close') { - if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.closeHandler) { - window.webkit.messageHandlers.closeHandler.postMessage('close'); - } + window.webkit?.messageHandlers?.closeHandler?.postMessage('close'); } });