I am developing a swift ios app that uses WKWebView to load up an ecommerce site.
When a user purchases a product here, the checkout page allows the user to pay in cryptocurrency.
When the user clicks "Open in Wallet", the site shoots off a
window.postMessage(paymentData)
where payment data is a js object with a bitcoin url in it.
I am using a WKUserScript with WKWebConfiguration to inject a script that listens for a window message and then fires off data to my webkit.messageHandler.
let source = """
window.addEventListener('message', function(e) { window.webkit.messageHandlers.iosListener.postMessage(JSON.stringify(e.data)) } )
"""
Unfortunately this code never triggers.
When I use chrome or safari devtools to inject the same javascript, it works just fine.
I have scoured stack overflow to see if there is a special condition for window.postMessage in WKWebView but have had no luck thus far.
Is it possible to capture a window.postMessage() event and pipe the event data back to my ios app?
Thanks in advance!!!! Here is my existing code.
let webConfiguration = WKWebViewConfiguration()
let source = """
window.addEventListener('message', function(e) { window.webkit.messageHandlers.iosListener.postMessage(JSON.stringify(e.data)) } )
"""
let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
userContentController.addUserScript(script)
userContentController.add(self, name: "iosListener")
webConfiguration.userContentController = userContentController
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(webView)
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
print("message body: \(message.body)")
print("message frameInfo: \(message.frameInfo)")
}