我的应用程序遇到问题,无法使用 Ionic Native 的应用程序启动器在 Android 上打开另一个应用程序。
canOpenUrl方法返回true,表示该URL可以打开。但是,当尝试使用 openUrl 方法导航到 Android 应用程序时,会失败。有趣的是,如果我使用不带任何参数的方法,应用程序将成功打开。仅在传递数据时才会出现问题。
我已遵循官方文档中概述的所有步骤,网址为https://ionicframework.com/docs/native/app-launcher
框架:
代码:
import { AppLauncher } from '@capacitor/app-launcher';
const checkCanOpenUrl = async () => {
const { value } = await AppLauncher.canOpenUrl({ url: 'com.getcapacitor.myapp' });
console.log('Can open URL: ', value); // Returns true
};
const openPortfolioPage = async () => { // Fails to open the application
await AppLauncher.openUrl({ url: 'com.getcapacitor.myapp://page?id=portfolio' });
};
我想要导航,也想将数据传递到 Android 本机应用程序。
传递数据时,请确保在接收应用程序的 AndroidManifest.xml 文件的 Intent 过滤器中指定自定义方案。您可以通过将以下意图过滤器添加到活动标记来实现此目的
示例代码:
<activity
android:name=".activity.MainActivity"
android:exported="true"
android:launchMode="singleTask"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- below is an scheme-->
<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="com.getcapacitor.myapp" />
</intent-filter>
<!-- -->
</activity>
通过在 Intent 过滤器中添加该行,您可以指定可以使用具有指定自定义方案 (com.getcapacitor.myapp) 的 URL 启动该活动。这确保接收应用程序可以正确处理传递给它的数据。