Flutter打開外部應(yīng)用可以使用url_launcher這個庫
url_launcher官方文檔 https://pub.dev/packages/url_launcher
1、dependencies: url_launcher: ^5.4.1
2屹篓、引入庫
import ‘package:url_launcher/url_launcher.dart’;
3峭弟、Flutter中打開外部應(yīng)用
RaisedButton(
child: Text('打開外部應(yīng)用'),
onPressed: () async{
// weixin://
const app = 'alipays://';
if (await canLaunch(app)) {
await launch(app);
} else {
throw 'Could not launch $app';
}
},
)
打開其他App
關(guān)于打開鏈接跳纳,電話,短信和電子郵件等方式在上面表格中有寫晕粪,使用方法跟例子一樣倡缠,不再贅述寇僧。
下面我們看一下怎么打開手機中的其他App。
想要打開其他app妄痪,需要知道被打開app的scheme, 如果是自己的app哈雏,Android可以在Manifest中看到:
<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:host="li.zhuoyuan"
android:scheme="flutter" />
</intent-filter>
上面<data>標(biāo)簽中的就是我們需要知道的url中的東西, 定義了scheme為flutter, host為li.zhuoyuan. 記住這兩個字段,在我們想打開這個app的地方需要裳瘪⊥两可以只定義scheme ,host為可選項彭羹。
那么黄伊,我們需要的url組成部分就是:scheme://host 如果有host的話。
注意:這里scheme為必填派殷,host还最、path為選填。選填內(nèi)容可以不填毡惜,但是一旦填上了就必須全部完全匹配才會成功拉起拓轻。
我們需要打開的地方執(zhí)行代碼為:
void _openOtherApp() async {
const url = 'flutter://li.zhuoyuan'; //這個url就是由scheme和host組成的 :scheme://host
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}