@capacitor/app
The App API handles high level App state and events. For example, this API emits events when the app enters and leaves the foreground, handles deeplinks, opens other apps, and manages persisted plugin state.
Install
npm install @capacitor/app
npx cap sync
iOS
For being able to open the app from a custom scheme you need to register the scheme first. You can do it by editing the Info.plist file and adding this lines.
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.getcapacitor.capacitor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>mycustomscheme</string>
</array>
</dict>
</array>
Android
For being able to open the app from a custom scheme you need to register the scheme first. You can do it by adding this lines inside the activity section of the AndroidManifest.xml.
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/custom_url_scheme" />
</intent-filter>
custom_url_scheme value is stored in strings.xml. When the Android platform is added, @capacitor/cli adds the app's package name as default value, but can be replaced by editing the strings.xml file.
Example
import { App } from '@capacitor/app';
App.addListener('appStateChange', ({ isActive }) => {
console.log('App state changed. Is active?', isActive);
});
App.addListener('appUrlOpen', data => {
console.log('App opened with URL:', data);
});
App.addListener('appRestoredResult', data => {
console.log('Restored state:', data);
});
const checkAppLaunchUrl = async () => {
const { url } = await App.getLaunchUrl();
console.log('App opened with URL: ' + url);
};
Configuration
| Prop | Type | Description | Default | Since |
|---|---|---|---|---|
disableBackButtonHandler | boolean | Disable the plugin's default back button handling. Only available for Android. | false | 7.1.0 |
Examples
In capacitor.config.json:
{
"plugins": {
"App": {
"disableBackButtonHandler": true
}
}
}
In capacitor.config.ts:
/// <reference types="@capacitor/app" />
import { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
plugins: {
App: {
disableBackButtonHandler: true,
},
},
};
export default config;
API
exitApp()getInfo()getState()getLaunchUrl()minimizeApp()getAppLanguage()toggleBackButtonHandler(...)addListener('appStateChange', ...)addListener('pause', ...)addListener('resume', ...)addListener('appUrlOpen', ...)addListener('appRestoredResult', ...)addListener('backButton', ...)removeAllListeners()- Interfaces
- Type Aliases
exitApp()
exitApp() => Promise<void>
Force exit the app. This should only be used in conjunction with the backButton handler for Android to
exit the app when navigation is complete.
Ionic handles this itself so you shouldn't need to call this if using Ionic.
Since: 1.0.0
getInfo()
getInfo() => Promise<AppInfo>
Return information about the app.
Returns: Promise<AppInfo>
Since: 1.0.0
getState()
getState() => Promise<AppState>
Gets the current app state.
Returns: Promise<AppState>
Since: 1.0.0
getLaunchUrl()
getLaunchUrl() => Promise<AppLaunchUrl | undefined>
Get the URL the app was launched with, if any.
Returns: Promise<AppLaunchUrl>
Since: 1.0.0
minimizeApp()
minimizeApp() => Promise<void>
Minimizes the application.
Only available for Android.
Since: 1.1.0
getAppLanguage()
getAppLanguage() => Promise<AppLanguageCode>
Get the app specific language locale code.
Returns: Promise<AppLanguageCode>
Since: 8.1.0
toggleBackButtonHandler(...)
toggleBackButtonHandler(options: ToggleBackButtonHandlerOptions) => Promise<void>
Enables or disables the plugin's back button handling during runtime.
Only available for Android.
| Param | Type |
|---|---|
options | ToggleBackButtonHandlerOptions |
Since: 7.1.0
addListener('appStateChange', ...)
addListener(eventName: 'appStateChange', listenerFunc: StateChangeListener) => Promise<PluginListenerHandle>
Listen for changes in the app or the activity states.
On iOS it's fired when the native UIApplication.willResignActiveNotification and UIApplication.didBecomeActiveNotification events get fired. On Android it's fired when the Capacitor's Activity onResume and onStop methods gets called. On Web it's fired when the document's visibilitychange gets fired.
| Param | Type |
|---|---|
eventName | 'appStateChange' |
listenerFunc | StateChangeListener |
Returns: Promise<PluginListenerHandle>
Since: 1.0.0
addListener('pause', ...)
addListener(eventName: 'pause', listenerFunc: () => void) => Promise<PluginListenerHandle>
Listen for when the app or the activity are paused.
On iOS it's fired when the native UIApplication.didEnterBackgroundNotification event gets fired. On Android it's fired when the Capacitor's Activity onPause method gets called. On Web it's fired when the document's visibilitychange gets fired and document.hidden is true.
| Param | Type |
|---|---|
eventName | 'pause' |
listenerFunc | () => void |
Returns: Promise<PluginListenerHandle>
Since: 4.1.0
addListener('resume', ...)
addListener(eventName: 'resume', listenerFunc: () => void) => Promise<PluginListenerHandle>