Skip to main content

@napacitor/browser

In-app browser plugin (corresponds to @capacitor/browser).

Installation​

To use npm:

npm install @napacitor/browser

To use yarn:

yarn add @napacitor/browser

Sync native files:

npx nap sync

Usage​

import { Browser } from '@napacitor/browser';

await Browser.open({ url: 'https://example.com', toolbarColor: '#3282F6' });

// Close the in-app browser (the MAUI Browser API has no cross-platform close entry point; the native side throws NOT_IMPLEMENTED)
await Browser.close();

// Event listeners (Android/iOS; require platform code, the native side does not dispatch them yet)
Browser.addListener('browserFinished', () => console.log('Browser closed'));
Browser.addListener('browserPageLoaded', () => console.log('Page loaded'));

// Remove all listeners
await Browser.removeAllListeners();

How It Works​

Wraps the APIs described in the .NET MAUI Browser documentation: Browser.Default.OpenAsync(uri, BrowserLaunchOptions) (SystemPreferred mode: Android Custom Tabs / iOS SFSafariViewController / falls back to an external browser on desktop). toolbarColor is parsed as PreferredToolbarColor (invalid color strings fall back to the platform default). The MAUI Browser API only provides OpenAsync; close and the browserFinished / browserPageLoaded events require platform code, which the native side does not implement yet (see API notes).

Permissions​

Android​

No additional configuration required.

iOS​

No additional configuration required.

Windows​

No additional configuration required.

API​

open(options)​

Opens an in-app browser page (SystemPreferred mode).

ParamType
optionsOpenOptions

Returns: Promise<void>

close()​

Closes the browser window. The MAUI Browser API has no cross-platform close entry point, so the native side throws NOT_IMPLEMENTED (closing Android Custom Tabs / iOS SFSafariViewController sessions requires a small amount of platform code); the web implementation calls window.close() on a best-effort basis (only windows opened by script can be closed).

Returns: Promise<void>

addListener('browserFinished', listenerFunc)​

Dispatched when the browser is closed by the user. Requires listening to Custom Tabs callbacks / the SFSafariViewController delegate; the native side does not dispatch it yet (a debug hint is logged when registering the listener).

ParamType
eventName'browserFinished'
listenerFunc() => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

addListener('browserPageLoaded', listenerFunc)​

Dispatched when the first page finishes loading. Same as above, the native side does not dispatch it yet (requires platform code).

ParamType
eventName'browserPageLoaded'
listenerFunc() => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

removeAllListeners()​

Removes all listeners of this plugin.

Returns: Promise<void>

Interfaces​

OpenOptions​

PropTypeDescription
urlstringAbsolute URL to open (must be a valid absolute address, otherwise INVALID_ARGUMENTS is thrown).
windowNamestringWeb-only: the window.open target (default _blank); ignored on other platforms.
toolbarColorstringToolbar color (#RRGGBB / #AARRGGBB); ignored where unsupported, invalid color strings fall back to the platform default toolbar color.
presentationStyle'fullscreen' | 'popover'iOS-only: presentation style; ignored on other platforms.
widthnumberiOS-only (iPad popover width); ignored on other platforms.
heightnumberiOS-only (iPad popover height); ignored on other platforms.