For the complete documentation index, see llms.txt. This page is also available as Markdown.

iOS

Send and receive journey events through a WKWebView bridge on iOS.

There is no separate native SDK or bridge shipped by Cover Genius for iOS. You relay the same JSON envelope described on the main Messaging page through WKWebView's standard message-handler mechanism, exactly as you would for any other web content.

This page shows the CG specific envelope and events; for the underlying bridge mechanism itself, see Apple's official documentation for WKScriptMessageHandler and WKUserContentController.

Prerequisites

The journey is loaded inside a WKWebView, per your mobile embedding setup. Messaging depends on the web view loading successfully and JavaScript being enabled (WKWebViewConfiguration().preferences.javaScriptEnabled = true, the default).

Receiving events from the journey

Register a script message handler and inject a bridge script that forwards window.postMessage calls from the journey's web content into native code:

import WebKit

class JourneyViewController: UIViewController, WKScriptMessageHandler {
    var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let contentController = WKUserContentController()
        contentController.add(self, name: "cgJourneyBridge")

        // Forward postMessage calls made inside the page to the native handler.
        let bridgeScript = WKUserScript(
            source: """
            window.addEventListener('message', function(event) {
                if (event.data && event.data.source === 'xcover-journeys') {
                    window.webkit.messageHandlers.cgJourneyBridge.postMessage(event.data);
                }
            });
            """,
            injectionTime: .atDocumentStart,
            forMainFrameOnly: true
        )
        contentController.addUserScript(bridgeScript)

        let config = WKWebViewConfiguration()
        config.userContentController = contentController

        webView = WKWebView(frame: view.bounds, configuration: config)
        view.addSubview(webView)
        webView.load(URLRequest(url: URL(string: "https://xcj.xcover.com/acme-travel/en/?country=AU")!))
    }

    func userContentController(_ userContentController: WKUserContentController,
                                didReceive message: WKScriptMessage) {
        guard message.name == "cgJourneyBridge",
              let body = message.body as? [String: Any],
              let type = body["type"] as? String else { return }

        let payload = body["payload"] as? [String: Any] ?? [:]

        switch type {
        case "CG_JOURNEY_READY":
            print("Journey mounted and ready")
        case "CG_JOURNEY_COMPLETE":
            print("Journey finished:", payload["outcome"] ?? "", payload["policy_id"] ?? "")
        default:
            break
        }
    }
}

Sending events into the journey

Evaluate JavaScript in the web view to dispatch a message event the journey's listener picks up:

Origin and security notes

The journey's own trusted-origin validation is designed for browser postMessage traffic (checked against event.origin). Inside a WKWebView bridge, there is no browser-level origin to spoof from outside your app.

The risk model shifts instead to this: only your app's own injected bridge script can call window.webkit.messageHandlers.cgJourneyBridge.postMessage, and only code running inside the loaded page (the journey itself) can trigger it.

Load only the journey URL provided for your integration, and do not load arbitrary or user-supplied URLs into a web view with this bridge attached.

Last updated

Was this helpful?