@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
}
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();
Add Listeners
import { InAppBrowser } from '@capacitor/inappbrowser';
await InAppBrowser.addListener('browserClosed', () => {
console.log("browser was closed.");
});
await InAppBrowser.addListener('browserPageLoaded', () => {
console.log("browser was loaded.");
});
Remove All Listeners
import { InAppBrowser } from '@capacitor/inappbrowser';
InAppBrowser.removeAllListeners();
API
openInWebView(...)
openInWebView(model: OpenInWebViewParameterModel) => Promise<void>
Opens the web content of the given URL in your mobile app using a custom web view within your application.
Param | Type | Description |
---|---|---|
model |
| The parameters to open the URL in the web view |
openInSystemBrowser(...)
openInSystemBrowser(model: OpenInSystemBrowserParameterModel) => Promise<void>
Opens the web content of the given URL in your mobile app, using SafariViewController for iOS and Custom Tabs for Android.
Param | Type | Description |
---|---|---|
model |
| The parameters to open the URL in the system browser |
openInExternalBrowser(...)
openInExternalBrowser(model: OpenInDefaultParameterModel) => Promise<void>
Opens the web content of the given URL in a separate browser, outside of your mobile application.
Param | Type | Description |
---|---|---|
model |
| The parameters to open the URL in the external browser |
close()
close() => Promise<void>
Closes the currently active browser. It can be used to close browsers launched through the openInSystemBrowser or openInWebView actions.
addListener('browserClosed' | 'browserPageLoaded', ...)
addListener(eventName: 'browserClosed' | 'browserPageLoaded', listenerFunc: () => void) => Promise<PluginListenerHandle>
Adds a listener for the specified browser event.
Param | Type | Description |
---|---|---|
eventName | 'browserClosed' | 'browserPageLoaded' | The name of the browser event to listen for: 'browserClosed' or 'browserPageLoaded'. |
listenerFunc | () => void | The function to be called when the event occurs. |
Returns:
Promise<PluginListenerHandle>
removeAllListeners()
removeAllListeners() => void
Removes all listeners for the browser events.
Interfaces
OpenInWebViewParameterModel
Defines the options for opening a URL in the web view.
Prop | Type | Description |
---|---|---|
options |
| A structure containing some configurations to apply to the Web View. |