@capacitor/inappbrowser
The InAppBrowser Plugin provides a web browser view that allows you to load any web page externally. It behaves as a standard web browser and is useful to load untrusted content without risking your application's security. It offers three different ways to open URLs; in a WebView, in an in-app system browser (Custom Tabs for Android and SFSafariViewController for iOS), and in the device's default browser.
Install
npm install @capacitor/inappbrowser
npx cap sync
Supported Platforms
- iOS
- Android
Android
The InAppBrowser plugin requires a minimum Android SDK target of 26. This is higher than the default that comes with your Capacitor application. You can update this value in your android/variables.gradle file.
ext {
minSdkVersion = 26
}
LocalStorage Isolation
The openInWebView option provides isolation for localStorage and cookies to ensure that content loaded in the InAppBrowser does not interfere with the main application's storage.
- iOS: Storage is isolated by default.
- Android (API 28+): Storage is isolated by default by running the InAppBrowser in a separate process (
:OSInAppBrowser) with a dedicated data directory suffix. - Android (API < 28): Storage is shared with the main application due to platform limitations.
Opting-out of Isolation (Android)
If your use case requires sharing localStorage or cookies between the main app and the InAppBrowser on Android, you can opt-out of isolation by setting isIsolated: false in the android options.
Disabling isolation reduces the security of your app by allowing potentially untrusted web content to access your application's private storage (Cookies, LocalStorage, etc.). Use this only if absolutely necessary.
Breaking Change (Android): Apps upgrading to this version will lose any existing localStorage or cookies previously stored by the InAppBrowser on the first run. This is because the WebView now runs in a separate process with its own data directory. Users may need to re-authenticate with websites that relied on persisted session data.
Usage Example
Open In External Browser
import { InAppBrowser } from '@capacitor/inappbrowser';
await InAppBrowser.openInExternalBrowser({
url: "https://www.google.com"
});
Open In System Browser (Custom Tabs for Android, SFSafariViewController for iOS)
import { InAppBrowser, DefaultSystemBrowserOptions } from '@capacitor/inappbrowser';
await InAppBrowser.openInSystemBrowser({
url: "https://www.google.com",
options: DefaultSystemBrowserOptions
});
Open In Web View
import { InAppBrowser, DefaultWebViewOptions } from '@capacitor/inappbrowser';
await InAppBrowser.openInWebView({
url: "https://www.google.com",
options: DefaultWebViewOptions
});
Close (Web View or System Browser)
import { InAppBrowser } from '@capacitor/inappbrowser';
await InAppBrowser.close();