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

Flutter中使用WhatsApp Launcher URL发消息,iOS平台遇通道连接错误

iOS平台使用launchUrl打开WhatsApp的解决方案

1. 配置Info.plist(核心修复步骤)

iOS系统要求必须提前声明要跳转的外部应用Scheme,否则会触发通道连接失败的错误。

  • 打开项目的ios/Runner/Info.plist文件
  • 在<dict>标签内添加以下配置:
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>whatsapp</string>
</array>

2. 优化URL格式与调用逻辑

确保手机号格式合规,同时优化调用逻辑提升稳定性:

import 'package:url_launcher/url_launcher.dart';

Future<void> openWhatsApp(String phoneNumber) async {
  // 清理手机号:移除所有非数字字符,确保带国际区号
  final cleanedNumber = phoneNumber.replaceAll(RegExp(r'\D'), '');
  final Uri url = Uri.parse('whatsapp://send?phone=$cleanedNumber');

  try {
    if (await canLaunchUrl(url)) {
      await launchUrl(
        url,
        mode: LaunchMode.externalApplication, // 指定以外部应用方式打开
      );
    } else {
      print('设备未安装WhatsApp');
    }
  } catch (e) {
    print('跳转失败:$e');
  }
}

3. 额外注意事项

  • 确保url_launcher依赖包使用最新版本,避免兼容性问题
  • 必须使用真实iOS设备测试,模拟器无法正常跳转外部应用
  • 检查Xcode项目的签名配置是否正常,避免权限异常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 11:29:53