@capacitor/app
アプリAPIは、ハイレベルなアプリの状態とイベントを処理します。例えば、このAPIは、アプリがフォアグラウンドに入ったり出たりしたときにイベントを発行したり、ディープリンクを処理したり、他のアプリを開いたり、永続化されたプラグインの状態を管理したりします。
Install
npm install @capacitor/app
npx cap sync
iOS
カスタムスキームからアプリを開けるようにするには、まずスキームを登録する必要があります。Info.plistファイルを編集して以下の行を追加することで設定できます。
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.getcapacitor.capacitor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>mycustomscheme</string>
</array>
</dict>
</array>
Android
カスタムスキームからアプリを開けるようにするには、まずスキームを登録する必要があります。AndroidManifest.xmlのactivityセクション内に以下の行を追加することで設定できます。
<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の値はstrings.xmlに保存されます。Androidプラットフォームを追加すると、@capacitor/cliはアプリのパッケージ名をデフォルト値として追加しますが、strings.xmlファイルを編集することで変更できます。
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);
};
設定
| プロパティ | 型 | 説明 | デフォルト | Since |
|---|---|---|---|---|
disableBackButtonHandler | boolean | プラグインのデフォルトの戻るボタン処理を無効にします。Androidのみで使用可能です。 | false | 7.1.0 |
設定例
capacitor.config.jsonでの設定:
{
"plugins": {
"App": {
"disableBackButtonHandler": true
}
}
}
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()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>