Skip to content

WebView messaging

Start with a named WebView. This example uses its bridge for a low-risk signal: tell the host when an embedded help panel is ready.

Use the parent window on Web and the Native bridge when the page is running inside a Native WebView. Replace the target origin below with the origin of the Hozo app, not the origin of the embedded page.

js
const message = { type: 'ready' }if (window.ReactNativeWebView) {  window.ReactNativeWebView.postMessage(JSON.stringify(message))} else if (window.parent !== window) {  window.parent.postMessage(message, 'https://app.example.com')}

On Web, Hozo delivers only events from this iframe’s window. That window can navigate, so check origin against the origin you expect and check the payload. Web data is passed through as sent.

tsx
import { Platform } from '@hozo/rn-compat'import { WebView } from '@hozo/webview'export function HelpPanel({ onReady }: { onReady: () => void }) {  return (    <WebView      src="https://help.example.com/panel"      title="Help panel"      onMessage={({ origin, data }) => {        if (Platform.OS === 'web' && origin !== 'https://help.example.com') return        if (typeof data === 'object' && data !== null && 'type' in data && data.type === 'ready') {          onReady()        }      }}      className="h-96 w-full"    />  )}

Native parses the posted string as JSON when possible; otherwise it delivers the string. It does not provide origin or apply the iframe policies. Use a page you control for this example. A missing origin is not evidence of trust, and the declared src is not proof of where a message came from after navigation. Do not treat a bridge message as proof of a payment, a login, or a permission. Confirm those through the authoritative service instead.

If the Native app did not install react-native-webview, the browser link can still open a remote src, but it will not call the app’s onMessage. Keep any essential action usable without the readiness signal.