flutter 移动应用程序中打开URL

url_launcher: ^6.2.5
在Flutter中,url_launcher库是用于在移动应用程序中打开URL的常用工具。它允许你通过调用系统的浏览器或其他应用程序来打开指定的URL,比如网页链接、电子邮件链接、电话号码等。这个库提供了一种简单的方法来实现在应用中跳转到外部链接的功能,增强了应用的交互性和实用性。

launch方法: 用于启动任意URL,包括网址、电话号码、电子邮件地址等。
复制代码
// 启动网址
 _launchURL() async {
      final Uri url = Uri.parse('https://www.baidu.com/?tn=68018901_16_pg');
            if (!await launchUrl(url,mode:LaunchMode.externalApplication)) {
              throw Exception('Could not launch $url');
            }else {
      throw '无法打开链接 $url';
    }
  }
启动外部app
复制代码
// 启动微信APP
 _launchURL() async {
  final Uri url = Uri.parse('weixin://');
          if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
            throw Exception('Could not launch $url');
          }
  }
launchEmail方法: 用于启动发送电子邮件的操作
复制代码
//发送邮件
 _launchURL() async {
           // 收件人邮箱
          String recipient = "*******@qq.com";
          //邮件主题
          String subject = "邮件主题";
          // 邮件内容
          String body = "邮件内容";
          String mailtoUri =
              "mailto:$recipient?subject=$subject&body=$body";
          final Uri url = Uri.parse(mailtoUri);

          if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
            throw Exception('Could not launch $mailtoUri');
          }
  }
launchPhone方法: 用于启动拨打电话的操作
复制代码
//拨打电话
 _launchURL() async {
    String phoneNumber = "15818221700";
    final Uri launchUri = Uri(
      scheme: 'tel',
      path: phoneNumber,
    );
    await launchUrl(launchUri);
  }
发送短信
复制代码
//发送短信
 _launchURL() async {
          // 收件人电话
          String recipient = "151700";
          // 短信内容
          String body = "111";
          String smsUrl = 'sms:$recipient?body=${Uri.encodeQueryComponent(body)}';
          final Uri url = Uri.parse(smsUrl);

          if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
            throw Exception('Could not launch $smsUrl');
          }
  }