Skip to content

Commit

Permalink
Merge pull request #241 from klaviyo/ab/CHNL-14729/handle-close-events
Browse files Browse the repository at this point in the history
handle WKWebView `close` event
  • Loading branch information
ab1470 authored Dec 18, 2024
2 parents 18fcbd5 + 8ba7de1 commit 2fbebac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Sources/KlaviyoUI/KlaviyoWebView/KlaviyoWebViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -39,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")
}
}
}
13 changes: 13 additions & 0 deletions Sources/KlaviyoUI/KlaviyoWebView/Resources/Scripts/closeHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// closeHandler.js
// klaviyo-swift-sdk
//
// Created by Andrew Balmer on 12/18/24.
//


window.addEventListener("klaviyoForms", function(e) {
if (e.detail.type == 'close') {
window.webkit?.messageHandlers?.closeHandler?.postMessage('close');
}
});

0 comments on commit 2fbebac

Please sign in to comment.