For SwiftUI apps, BridgeWebView is the fastest path: drop it into a view, give it a URL and a host, and you have a fully wired bridge. Use the patterns below for state observation, child-view passing, and dynamic URLs.
For UIKit apps (or SwiftUI apps that need a custom WKWebViewConfiguration), use BridgeWebViewConfigurator to wire the bridge into a WebView you create yourself.
This approach is useful when you need custom WebView settings (media playback, data detection, process pools, etc.) that makeWebView doesnβt configure.
The BridgeHost and the WebView have different ownership semantics β the host holds a weak reference to the WebView, and handlers should be set up before content loads. Keep these in mind to avoid leaks, attachment errors, and missed messages.
The BridgeHost should live at least as long as the WebView. In SwiftUI, use @StateObject to ensure the host isnβt recreated on view updates. In UIKit, hold a strong reference as a property.
BridgeHost holds a weak reference to the WebView. If the WebView is deallocated while the host is still alive, attempts to send messages will set lastError to "WebView not attached". This is by design β it prevents retain cycles.
If you need to replace the WebView (e.g., after a navigation reset), call BridgeWebViewConfigurator.configure(_:host:) with the new WebView. The host automatically updates its internal reference.
Set handlers on typed slots or register custom capabilities before the WebView loads content. If the web journey sends a request before a handler is available, the request goes to pendingRequests.
// Set up typed slotshost.documentCapture.handler = { [weak host] request in guard let host else { return .cancelled(reason: "Host deallocated") } return await host.documentCapture.awaitCompletion()}// Register custom capabilitieshost.registerCustomCapability("nfc.read") { request, responder in // Handle NFC}// Then load the journeywebView.load(URLRequest(url: journeyURL))
BridgeWebView provides a basic WKNavigationDelegate via its Coordinator. If you need custom navigation behavior (e.g., intercepting links, handling authentication challenges), you have two options:
UIKit path: Use BridgeWebViewConfigurator and set your own navigation delegate on the WebView.
SwiftUI path: Build a custom UIViewRepresentable that uses BridgeWebViewConfigurator internally.