Skip to content

Runtime Detection

Use these exports to decide when bridge calls should run, and how to deliver the SDK in browser contexts.

Detection exports

ExportTypeNotes
isNativebooleanComputed once at module load
platformInfoobjectSnapshot at module load
hasMessageHandlers()functionChecks window.webkit.messageHandlers.pwakit
hasPWAKitInUserAgent()functionChecks PWAKit in user agent
detectPlatform()functionReturns ios, browser, or unknown
getPlatformInfo()functionRe-evaluates full detection result

Recommendation

For dynamic checks, prefer getPlatformInfo() rather than relying only on isNative.

Runtime behavior

EnvironmentDetectionBridge calls
PWAKit iOS shellgetPlatformInfo().isNative === trueAvailable
Regular browserusually isNative === falseReject with BridgeUnavailableError
SSR/Nodewindow absentNot available

Browser bundle (no bundler)

Use the IIFE bundle when you want script-tag usage:

html
<script src="https://cdn.jsdelivr.net/npm/@pwa-kit/sdk@latest/dist/index.global.js"></script>
<script>
  const { isNative, push } = PWAKit;
  if (isNative) {
    push.subscribe();
  }
</script>

The global variable is window.PWAKit.

Bridge events

PWABridge dispatches native events as browser CustomEvents with pwa: prefix.

Subscribe

ts
import { bridge } from "@pwa-kit/sdk";

const unsubscribe = bridge.on("push", (data) => {
  console.log("Push payload", data);
});

// Later
unsubscribe();

One-time listener

ts
bridge.once("push", (data) => {
  console.log("First push only", data);
});

Bridge call options

bridge.call(module, action, payload, options) supports:

  • options.timeout (ms)

Default timeout is 30000 ms.

Fallback pattern

ts
import { BridgeUnavailableError, getPlatformInfo, ios } from "@pwa-kit/sdk";

export async function authenticateIfNative(): Promise<boolean> {
  if (!getPlatformInfo().isNative) return false;

  try {
    const result = await ios.biometrics.authenticate("Continue");
    return result.success;
  } catch (error) {
    if (error instanceof BridgeUnavailableError) return false;
    throw error;
  }
}

Released under the MIT License.