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

咨询Flutter实现仅在WhatsApp选择联系人分享图文的可行方案

解决方案:Flutter 实现仅 WhatsApp 内选联系人分享图文(跨 iOS/Android)

Android 端:通过 Intent 直接唤起 WhatsApp 分享界面

Android 可以构造指定 WhatsApp 包名的 Intent,直接唤起它的联系人选择页,同时传递文本和图片,完全不会出现其他平台选项。

实现步骤

  1. Flutter 侧用 MethodChannel 调用原生代码:
static const _channel = MethodChannel('whatsapp_custom_share');

Future<void> shareToWhatsApp(String text, String imagePath) async {
  try {
    await _channel.invokeMethod('shareWithWhatsApp', {
      'text': text,
      'imagePath': imagePath,
    });
  } on PlatformException catch (e) {
    print("分享失败: ${e.message}");
  }
}
  1. Android 原生 MainActivity 中处理方法调用:
class MainActivity : FlutterActivity() {
    private val CHANNEL = "whatsapp_custom_share"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "shareWithWhatsApp") {
                val text = call.argument<String>("text")
                val imagePath = call.argument<String>("imagePath")
                shareViaWhatsApp(text, imagePath, result)
            } else {
                result.notImplemented()
            }
        }
    }

    private fun shareViaWhatsApp(text: String?, imagePath: String?, result: MethodChannel.Result) {
        val shareIntent = Intent(Intent.ACTION_SEND)
        shareIntent.type = "image/*"
        shareIntent.putExtra(Intent.EXTRA_TEXT, text)
        
        // 用 FileProvider 生成可共享的图片 Uri(避免权限问题)
        val imageUri = FileProvider.getUriForFile(
            this,
            "${packageName}.fileprovider",
            File(imagePath!!)
        )
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri)
        
        // 指定仅 WhatsApp 处理这个 Intent
        shareIntent.setPackage("com.whatsapp")

        try {
            startActivity(shareIntent)
            result.success(null)
        } catch (e: ActivityNotFoundException) {
            result.error("NO_WHATSAPP", "未安装 WhatsApp", null)
        }
    }
}
  1. 配置 FileProvider(AndroidManifest.xml):
<application>
    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
</application>
  1. 在 res/xml 下创建 file_paths.xml(路径要和你的图片存储目录对应):
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="shared_images" path="Pictures/" />
</paths>

iOS 端:用 URL Scheme + 文档交互实现

iOS 要唤起 WhatsApp 联系人选择并分享图文,需要结合 UIDocumentInteractionController,同时配置 URL Scheme 白名单。

实现步骤

  1. Flutter 侧复用上面的 MethodChannel 调用代码即可。

  2. iOS 原生 ViewController 处理方法:

import UIKit
import Flutter

class MainViewController: FlutterViewController {
    private let CHANNEL = "whatsapp_custom_share"
    var docController: UIDocumentInteractionController?

    override func viewDidLoad() {
        super.viewDidLoad()
        let channel = FlutterMethodChannel(name: CHANNEL, binaryMessenger: binaryMessenger)
        channel.setMethodCallHandler { [weak self] call, result in
            guard let self = self else { return }
            if call.method == "shareWithWhatsApp" {
                guard let text = call.arguments?["text"] as? String,
                      let imagePath = call.arguments?["imagePath"] as? String else {
                    result.error("INVALID_ARGS", "参数错误", nil)
                    return
                }
                self.shareToWhatsApp(text: text, imagePath: imagePath, result: result)
            } else {
                result.notImplemented()
            }
        }
    }

    private func shareToWhatsApp(text: String, imagePath: String, result: FlutterResult) {
        let imageUrl = URL(fileURLWithPath: imagePath)
        docController = UIDocumentInteractionController(url: imageUrl)
        // 给图片附加文本说明
        docController?.annotation = ["WhatsApp caption": text]
        docController?.uti = "net.whatsapp.image"
        // 唤起 WhatsApp 的选择界面
        docController?.presentOpenInMenu(from: view.bounds, in: view, animated: true)
        result.success(null)
    }
}
  1. 在 Info.plist 中添加 WhatsApp Scheme 白名单:
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>whatsapp</string>
</array>

核心注意事项

  • 图片权限:Android 必须用 FileProvider 共享图片,iOS 确保图片在应用沙盒或临时目录内,不然 WhatsApp 访问不到。
  • 版本兼容:确保用户安装的 WhatsApp 是较新版本,旧版本可能不支持同时分享图文。
  • 错误处理:一定要捕获未安装 WhatsApp 的情况,给用户明确提示。

简化方案(不写原生代码)

如果不想写原生代码,可以尝试用 url_launcher 唤起 WhatsApp,但图片传递不太稳定:

  • Android 端:
await launchUrl(
  Uri.parse("intent://send/#Intent;package=com.whatsapp;text=$text;type=image/*;end"),
  mode: LaunchMode.externalApplication,
);
  • iOS 端:只能先唤起 WhatsApp,文本可以通过 Scheme 传递,但图片需要手动选择,所以还是原生方案更可靠。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 14:53:22