@capacitor/app
アプリAPIは、ハイレベルなアプリの状態とイベントを処理します。例えば、このAPIは、アプリがフォアグラウンドに入ったり出たりしたときにイベントを発行したり、ディープリンクを処理したり、他のアプリを開いたり、永続化されたプラグインの状態を管理したりします。
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);
};