废话不多 代码里说
方法1:使用url_launcher:
- 第一步:pubspec.yaml 添加 url_launcher: ^5.0.3
- 第二步 package get
- 第三步 import 'package:url_launcher/url_launcher.dart';
- 第四步 代码
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
//URL是一个字符串 一般是 scheme + host
但是我在使用这个方法的时候有部分APP使用不了 可能是这些APP的主页面没有设置scheme和host吧 所以我使用了下面这种方式
方法2:使用linker
- 第一步:pubspec.yaml 添加 linker: 0.0.2
- 第二步 package get
- 第三步 import 'package:linker/linker.dart';
- 第四步 代码
if (Platform.isAndroid) {//判断是否是android平台
try {
await Linker.startActivity(new Intent.callApp(packageName: 包名,className: activity的绝对路径));
} on PlatformException catch (e) {
print("Open failed $e");
}
}
这种方法是通过报名打开的 同时也支持iOS 但是不是用报名(iOS没有包名不是) 下面我会继续写打开公众号 所以以后会更新
2020.05.18更
MapleLuv
根据这位老铁提醒,更新
关于Android的配置:
如果要打开自己的app,需要在自己的Android工程的AndroidManifest.xml中配置:
<!--通过浏览器Url启动app-->
<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="lolita"
android:scheme="night" />
<!--<a href="[scheme]://[host]/[path]?[query]">启动应用程序</a>-->
</intent-filter>
关于iOS的配置
1)需要在Xcode里,target>info>URL Type配置URL Schames:myapp;
2)需要在info.plist里面配置白名单:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>myapp</string>
</array>