You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Web使用url_launcher在部分Safari无法正常发送SMS

Flutter Web:url_launcher发送SMS在部分Safari浏览器格式异常问题

问题表现

  • 安卓浏览器中使用url_launcher发送SMS完全正常
  • 部分iOS设备的Safari浏览器里SMS内容格式异常,两款iOS设备测试结果不一致,仅部分Safari存在该问题,安卓端无异常

Flutter Web使用url_launcher在部分Safari无法正常发送SMS

当前代码实现

void sendSmsWeb(String message) async {
  PrefManage prefManage = PrefManage();

  prefManage.getSelectedPhonePref().then((value) async {
    selectedPhone = value;
    if (selectedPhone != "") {
      String uri = 'sms:$selectedPhone?body=${Uri.encodeComponent(message)}';
      await launchUrl(Uri.parse(uri));
    } else {
      toast("no device found");
    }
  });
}

已尝试的无效方案

  • 区分平台使用不同参数分隔符:安卓用?、iOS用&,问题未解决

可行解决方案建议

1. 适配Safari的SMS URI格式

Safari对SMS协议的解析逻辑和安卓浏览器有差异,传统的?body=格式在部分版本的Safari中会被错误解析。可以改用smsto:协议,并通过;分隔号码和body参数:

void sendSmsWeb(String message) async {
  PrefManage prefManage = PrefManage();
  final selectedPhone = await prefManage.getSelectedPhonePref();
  
  if (selectedPhone.isNotEmpty) {
    Uri smsUri;
    // 通过用户代理判断是否为iOS/Safari环境
    final isIOS = html.window.navigator.userAgent.contains('iPhone') || html.window.navigator.userAgent.contains('iPad');
    if (isIOS) {
      // Safari适配格式
      smsUri = Uri.parse('smsto:$selectedPhone;body=${Uri.encodeComponent(message)}');
    } else {
      // 安卓及其他浏览器通用格式
      smsUri = Uri.parse('sms:$selectedPhone?body=${Uri.encodeComponent(message)}');
    }
    // 指定外部应用打开,避免浏览器内解析异常
    await launchUrl(smsUri, mode: LaunchMode.externalApplication);
  } else {
    toast("no device found");
  }
}

注:需要导入dart:html包来获取用户代理信息

2. 确保url_launcher为最新版本

旧版本的url_launcher可能存在Web端的适配问题,更新到最新版本能修复部分已知bug:

# pubspec.yaml中更新依赖
dependencies:
  url_launcher: ^6.2.5

3. 优化Web端配置

在web/index.html中添加iOS相关meta标签,帮助Safari正确识别应用环境:

<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">

内容的提问来源于stack exchange,提问作者Amin Azizzadeh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 09:42:33