简介
用于在移动平台中启动URL
的Flutter
插件,适用于IOS
和Android
平台。
可以打开网页、发送邮件、拨打电话、发送短信。
通常需要打开类似于淘宝、支付宝、微信、京东等APP
, 打开这一类的APP
需要知道对应APP
的url schema
, 即可顺滑的打开三方APP
- 支持的
URL
方案
方案 | Action |
---|---|
http:<URL> ,https:<URL> 例如 http://flutter.dev |
在默认浏览器中打开URL |
mailto:<email address>?subject=<subject>&body=<body> ,例如 mailto:smith@example.org?subject=News&body=New%20plugin |
在默认电子邮件应用中创建电子邮件 |
tel:<phone number> , 例如 tel:+1 555 010 999 |
拨打电话以使用默认电话应用程序 |
sms:<phone number> , 例如 sms:5550101234 |
使用默认消息传递应用程序发送SMS消息 |
使用
- 修改
pubspec.yaml
文件,添加依赖url_launcher
# ...
dev_dependencies:
# ...
url_launcher: ^5.7.2
# ...
- 在代码中使用
_launchUrl(url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
// 打开网页
_launchUrl('https://www.baidu.com');
// 发送邮件
final Uri _emailUri = Uri(
scheme: 'mailto',
path: '2077670161@qq.com',
queryParameters: {
'subject': 'Example Subject & Symbols are allowed!'
}
);
_launchUrl(_emailUri.toString());
// 拨打电话
_launchUrl('tel:13612345678');
// 发送邮件
_launchUrl('sms:13612345678');
// 打开微信
_launchUrl('weixin://');
发表评论