This guide explains how to send events to the web journey, handle incoming requests, and build request-response flows.Documentation Index
Fetch the complete documentation index at: https://docs.go.gbgplc.com/llms.txt
Use this file to discover all available pages before exploring further.
Message Types Overview
GBGBridge uses three message types:| Type | Direction | Purpose |
|---|---|---|
| Request | Web β Native | The web journey asks the host to do something. Expects a response. |
| Response | Native β Web | The host returns the result of a request. |
| Event | Either direction | An asynchronous notification. No response expected. |
BridgeMessage) with a correlationId for pairing requests and responses.
Sending Events to the Web Journey
Events are fire-and-forget messages from the native host to the web journey. Use them to notify the web journey of state changes, user actions, or lifecycle transitions.Common Event Patterns
| Event Action | When to Send | Example Data |
|---|---|---|
host.ready | After host initialization completes | { timestamp } |
host.background | App enters background | {} |
host.foreground | App returns to foreground | {} |
journey.cancel | User taps a cancel/back button | { reason } |
host.orientation | Device orientation changes | { orientation } |
Handling Incoming Requests
When the web journey sends a request, GBGBridge routes it to a registered handler or adds it topendingRequests.
Using Typed Capability Slots (Recommended)
For capture operations, use the typed slots onBridgeHost. Setting a handler declares support and routes requests automatically:
CaptureResult into the bridge protocol format automatically. See the Capability Handling Guide for the full SwiftUI integration pattern.
Using Custom Capabilities or BridgeCapabilityHandler
For non-camera capabilities, useregisterCustomCapability() or implement BridgeCapabilityHandler:
Handling Requests via the Delegate
For requests that donβt map to a single handler β or when you want centralized request handling β useBridgeHostDelegate.
Responding to Pending Requests Manually
If a request arrives with no registered handler, it is stored inpendingRequests. You can respond to it later.
Response Patterns
When responding to a request, you supply a status, optional data, and an optional error payload. The patterns below cover the three response shapes youβll use most: success-with-data, error, and cancellation.Success with Data
Error with Details
User Cancellation
Unsupported Action
Acknowledged (Async Processing)
Use.acknowledged when the operation will take time and you want to inform the web journey that the request was received.
Observing All Messages
UseBridgeHostDelegate.bridgeHost(_:didReceive:) to observe every message, or subscribe to the receivedMessages publisher via Combine.
Error Handling
Bridge messaging can fail in three distinct places: when GBGBridge canβt decode an incoming message, when no WebView is attached to receive an outgoing one, and when JavaScript evaluation in the WebView itself errors out. Each surfaces throughlastError so you can react in your UI.
Encoding/Decoding Errors
If GBGBridge cannot decode an incoming message,lastError is set with a description. The message is not added to receivedMessages.
WebView Not Attached
If you callsend(event:data:) or respond(to:...) before attaching a WebView, lastError is set to "WebView not attached".
JavaScript Evaluation Errors
If the JavaScript evaluation fails (e.g., the web page has navigated away or thereceive() function is not defined), lastError is set with the WebKit error description.
Monitor errors reactively:
Next Steps
- Capability Handling Guide β Build handlers for camera, NFC, and other features
- Embedding Guide β SwiftUI and UIKit integration patterns
- API Reference β Detailed method signatures and parameters