> For the complete documentation index, see [llms.txt](https://partner-docs.covergenius.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://partner-docs.covergenius.com/xcover-journeys/integration/messaging/ios.md).

# iOS

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

{% hint style="info" %}
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](https://developer.apple.com/documentation/webkit/wkscriptmessagehandler) and [WKUserContentController](https://developer.apple.com/documentation/webkit/wkusercontentcontroller).
{% endhint %}

### 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:

```swift
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:

```swift
func sendToJourney(type: String, payload: [String: Any] = [:]) {
    let payloadData = try! JSONSerialization.data(withJSONObject: payload)
    let payloadJSON = String(data: payloadData, encoding: .utf8)!

    let js = """
    window.postMessage({ source: 'xcover-journeys', type: '\(type)', payload: \(payloadJSON) }, '*');
    """
    webView.evaluateJavaScript(js)
}

// Example: ask the journey to hide itself
sendToJourney(type: "CG_JOURNEY_HIDE")
```

### 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://partner-docs.covergenius.com/xcover-journeys/integration/messaging/ios.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
