Getting Started
-
Install the package
Terminal window npm i @capgo/inappbrowserTerminal window pnpm add @capgo/inappbrowserTerminal window yarn add @capgo/inappbrowserTerminal window bun add @capgo/inappbrowser -
Sync with native projects
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync
Import the plugin and use its two main entry points:
import { InAppBrowser, ToolBarType, BackgroundColor } from '@capgo/inappbrowser';
// 1) Simple custom tab / SFSafariViewControllerconst openExternal = async () => { await InAppBrowser.open({ url: 'https://capgo.app', isPresentAfterPageLoad: true, preventDeeplink: false, });};
// 2) Full WebView with navigation, headers, share, messaging, etc.const openWebView = async () => { await InAppBrowser.openWebView({ url: 'https://capgo.app', title: 'Capgo', toolbarType: ToolBarType.NAVIGATION, backgroundColor: BackgroundColor.BLACK, activeNativeNavigationForWebview: true, showReloadButton: true, shareSubject: 'Check this page', shareDisclaimer: { title: 'Disclaimer', message: 'You are about to share content', confirmBtn: 'Continue', cancelBtn: 'Cancel', }, });};
// Messaging between app and the opened WebViewconst setupListeners = async () => { await InAppBrowser.addListener('urlChangeEvent', (event) => { console.log('URL changed to:', event.url); });
await InAppBrowser.addListener('messageFromWebview', (event) => { console.log('Message from web:', event.detail); });
await InAppBrowser.addListener('closeEvent', () => { console.log('WebView closed'); });};
// Send data to the WebViewconst sendData = async () => { await InAppBrowser.postMessage({ detail: { action: 'refresh-profile' } });};
// Close and reload helpersconst closeBrowser = () => InAppBrowser.close();const reloadPage = () => InAppBrowser.reload();API Reference
Section titled âAPI Referenceâopen(options: OpenOptions)
Section titled âopen(options: OpenOptions)âOpens a URL in a custom tab / SFSafariViewController.
interface OpenOptions { /** Target URL to load */ url: string; /** Present after the page finishes loading */ isPresentAfterPageLoad?: boolean; /** Prevent deep links from opening external apps */ preventDeeplink?: boolean;}
await InAppBrowser.open({ url: 'https://example.com', preventDeeplink: true });openWebView(options: OpenWebViewOptions)
Section titled âopenWebView(options: OpenWebViewOptions)âLoads a full-featured WebView with navigation UI, headers, credentials, scripting and messaging.
interface OpenWebViewOptions { url: string; headers?: Record<string, string>; credentials?: { username: string; password: string }; materialPicker?: boolean; shareDisclaimer?: { title: string; message: string; confirmBtn: string; cancelBtn: string; }; toolbarType?: ToolBarType; shareSubject?: string; title?: string; backgroundColor?: BackgroundColor; activeNativeNavigationForWebview?: boolean; disableGoBackOnNativeApplication?: boolean; isPresentAfterPageLoad?: boolean; isInspectable?: boolean; isAnimated?: boolean; showReloadButton?: boolean; closeModal?: boolean; closeModalTitle?: string; closeModalDescription?: string; closeModalOk?: string; closeModalCancel?: string; visibleTitle?: boolean; toolbarColor?: string; toolbarTextColor?: string; showArrow?: boolean; ignoreUntrustedSSLError?: boolean; preShowScript?: string; preShowScriptInjectionTime?: 'documentStart' | 'pageLoad'; proxyRequests?: string; buttonNearDone?: { ios: { iconType: 'sf-symbol' | 'asset'; icon: string }; android: { iconType: 'asset' | 'vector'; icon: string; width?: number; height?: number }; }; textZoom?: number; preventDeeplink?: boolean; authorizedAppLinks?: string[]; enabledSafeBottomMargin?: boolean; useTopInset?: boolean; enableGooglePaySupport?: boolean; blockedHosts?: string[]; width?: number; height?: number; x?: number; y?: number;}
await InAppBrowser.openWebView({ url: 'https://new-page.com', toolbarType: ToolBarType.NAVIGATION, showReloadButton: true,});ToolBarType values: activity (close + share), compact (close only), navigation (back/forward + close), blank (no toolbar). BackgroundColor values: white or black.
close(options?)
Section titled âclose(options?)âCloses the WebView/custom tab.
Options:
isAnimated?: boolean- Whether to animate the close action
reload()
Section titled âreload()âReloads the current WebView page.
goBack()
Section titled âgoBack()âGoes back in WebView history and returns { canGoBack: boolean }.
setUrl({ url: string })
Section titled âsetUrl({ url: string })âReplaces the current WebView URL.
executeScript({ code: string })
Section titled âexecuteScript({ code: string })âInjects JavaScript into the WebView.
postMessage({ detail: Record<string, any> })
Section titled âpostMessage({ detail: Record<string, any> })âSends data from the native app to the WebView (receive in JS via window.addEventListener('messageFromNative', ...)).
getCookies({ url, includeHttpOnly? })
Section titled âgetCookies({ url, includeHttpOnly? })âReturns cookies for the URL.
clearCookies({ url }) / clearAllCookies() / clearCache()
Section titled âclearCookies({ url }) / clearAllCookies() / clearCache()âCookie and cache management helpers.
updateDimensions(options: DimensionOptions)
Section titled âupdateDimensions(options: DimensionOptions)âChange WebView size/position at runtime (width, height, x, y).
removeAllListeners()
Section titled âremoveAllListeners()âUnregister all listeners for the plugin.
urlChangeEvent
Section titled âurlChangeEventâFired when the URL changes in the browser.
interface UrlChangeEvent { url: string;}
InAppBrowser.addListener('urlChangeEvent', (event) => { console.log('New URL:', event.url);});messageFromWebview
Section titled âmessageFromWebviewâTriggered when window.mobileApp.postMessage(...) is called inside the WebView.
InAppBrowser.addListener('messageFromWebview', (event) => { console.log('Payload from web:', event.detail);});closeEvent
Section titled âcloseEventâFired when the browser is closed.
InAppBrowser.addListener('closeEvent', () => { console.log('Browser closed');});buttonNearDoneClick
Section titled âbuttonNearDoneClickâFired when the custom button added with buttonNearDone is pressed.
InAppBrowser.addListener('buttonNearDoneClick', (event) => { console.log('Button near done tapped', event);});confirmBtnClicked
Section titled âconfirmBtnClickedâTriggered when a confirm dialog (disclaimer or close modal) is accepted.
InAppBrowser.addListener('confirmBtnClicked', (event) => { console.log('Confirm accepted, current URL:', event.url);});browserPageLoaded / pageLoadError
Section titled âbrowserPageLoaded / pageLoadErrorâLifecycle events for WebView load success or failure.
InAppBrowser.addListener('browserPageLoaded', () => console.log('Page loaded'));InAppBrowser.addListener('pageLoadError', () => console.log('Page failed to load'));Advanced Usage
Section titled âAdvanced UsageâOAuth Flow Implementation
Section titled âOAuth Flow Implementationâimport { InAppBrowser } from '@capgo/inappbrowser';
export class OAuthService { private listeners: any[] = [];
async authenticate(authUrl: string, redirectUri: string) { return new Promise<string>((resolve, reject) => { // Listen for URL changes const urlListener = InAppBrowser.addListener('urlChangeEvent', (event) => { if (event.url.startsWith(redirectUri)) { // Extract OAuth code/token from URL const url = new URL(event.url); const code = url.searchParams.get('code');
if (code) { InAppBrowser.close(); resolve(code); } else { const error = url.searchParams.get('error'); reject(new Error(error || 'OAuth failed')); } } });
this.listeners.push(urlListener);
// Open OAuth provider InAppBrowser.open({ url: authUrl, preventDeeplink: true, }); }); }
cleanup() { this.listeners.forEach(listener => listener.remove()); this.listeners = []; }}Custom Browser UI
Section titled âCustom Browser UIâconst openCustomBrowser = async () => { await InAppBrowser.open({ url: 'https://example.com', isPresentAfterPageLoad: true, preventDeeplink: false, });};Handling External Links
Section titled âHandling External Linksâimport { InAppBrowser } from '@capgo/inappbrowser';
export class LinkHandler { async openExternalLink(url: string) { // Check if URL should open in browser if (this.shouldOpenInBrowser(url)) { await InAppBrowser.open({ url, preventDeeplink: true, }); } else { // Handle internally window.location.href = url; } }
private shouldOpenInBrowser(url: string): boolean { // External domains const externalDomains = ['youtube.com', 'twitter.com', 'facebook.com']; const urlDomain = new URL(url).hostname;
return externalDomains.some(domain => urlDomain.includes(domain)); }}Best Practices
Section titled âBest Practicesâ-
Always remove listeners
const listener = await InAppBrowser.addListener('urlChangeEvent', handler);// When donelistener.remove(); -
Handle browser states
let browserOpen = false;const launch = async () => {browserOpen = true;await InAppBrowser.openWebView({ url: 'https://example.com' });};InAppBrowser.addListener('closeEvent', () => {browserOpen = false;}); -
Validate URLs before opening
const isValidUrl = (url: string): boolean => {try {new URL(url);return true;} catch {return false;}};if (isValidUrl(url)) {await InAppBrowser.open({ url });} -
Customize for platform
import { Capacitor } from '@capacitor/core';const options = {url: 'https://example.com',preventDeeplink: Capacitor.getPlatform() === 'ios',};
Platform Notes
Section titled âPlatform Notesâ- Uses
SFSafariViewController - Supports iOS 11.0+
- Respects Safe Area insets
- Supports custom presentation styles
Android
Section titled âAndroidâ- Uses Chrome Custom Tabs
- Falls back to WebView if Chrome not available
- Supports Android 5.0 (API 21)+
- Toolbar customization supported via
toolbarType,toolbarColor,buttonNearDone, etc.
- Opens in new browser tab/window
- Limited customization options
- No URL change events