求Flutter Desktop macOS Platform Channel示例及支持情况确认
Flutter macOS Platform Channel 实现指南
我之前也碰到过这个情况——官方文档确实重点放在了Android和iOS上,macOS的Platform Channel示例确实不太好找,但其实它的实现逻辑和iOS非常接近,毕竟都是基于苹果的生态,下面我给你一步步拆解怎么弄:
1. Dart 层定义 Channel
和移动端的写法完全一致,先在你的Flutter代码里创建MethodChannel,作为两端通信的桥梁:
import 'package:flutter/services.dart'; class MacOSPlatformChannel { static const MethodChannel _channel = MethodChannel('com.your-app/macos-native'); // 示例方法:获取macOS系统版本 static Future<String?> getSystemVersion() async { try { final String? version = await _channel.invokeMethod('getSystemVersion'); return version; } on PlatformException catch (e) { return '获取版本失败: ${e.message}'; } } }
2. macOS 原生层实现 Channel 处理
打开Flutter项目里的macos/Runner目录,找到对应的原生文件(Swift或Objective-C),添加Channel的处理逻辑:
Swift 版本(推荐)
修改AppDelegate.swift:
import Cocoa import FlutterMacOS @NSApplicationMain class AppDelegate: FlutterAppDelegate { override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { return true } override func application(_ application: NSApplication, didFinishLaunchingWithOptions launchOptions: [NSApplication.LaunchOptionsKey : Any]? = nil) -> Bool { // 获取Flutter视图控制器 guard let flutterViewController = mainFlutterWindow?.contentViewController as? FlutterViewController else { return super.application(application, didFinishLaunchingWithOptions: launchOptions) } // 初始化MethodChannel let channel = FlutterMethodChannel(name: "com.your-app/macos-native", binaryMessenger: flutterViewController.engine.binaryMessenger) // 设置方法调用处理器 channel.setMethodCallHandler { call, result in switch call.method { case "getSystemVersion": // 返回macOS系统版本 let osVersion = ProcessInfo.processInfo.operatingSystemVersionString result(osVersion) default: // 处理未定义的方法 result(FlutterMethodNotImplemented) } } return super.application(application, didFinishLaunchingWithOptions: launchOptions) } }
Objective-C 版本
如果你的项目用的是Objective-C,修改AppDelegate.m:
#import "AppDelegate.h" #import <FlutterMacOS/FlutterMacOS.h> @implementation AppDelegate - (BOOL)application:(NSApplication *)application didFinishLaunchingWithOptions:(NSDictionary<NSApplicationLaunchOptionsKey, id> *)launchOptions { FlutterViewController* flutterVC = (FlutterViewController*)self.mainFlutterWindow.contentViewController; FlutterMethodChannel* channel = [FlutterMethodChannel methodChannelWithName:@"com.your-app/macos-native" binaryMessenger:flutterVC.engine.binaryMessenger]; [channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) { if ([call.method isEqualToString:@"getSystemVersion"]) { NSString* osVersion = [[NSProcessInfo processInfo] operatingSystemVersionString]; result(osVersion); } else { result(FlutterMethodNotImplemented); } }]; return [super application:application didFinishLaunchingWithOptions:launchOptions]; } - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender { return YES; } @end
3. 在Flutter中调用原生方法
在你的Flutter页面里调用刚才定义的方法即可,比如:
FutureBuilder<String?>( future: MacOSPlatformChannel.getSystemVersion(), builder: (context, snapshot) { return Text('当前macOS版本: ${snapshot.data ?? "加载中..."}'); }, )
额外注意事项
- 如果你需要传递复杂参数(比如自定义对象),和移动端一样可以使用
StandardMessageCodec或者自定义Codec,macOS端的参数解析逻辑和iOS完全一致。 - 对于**事件通道(EventChannel)**的实现,逻辑也和iOS相同:在macOS端创建
FlutterEventChannel并设置FlutterStreamHandler,就能实现原生向Flutter发送持续事件。 - 确保Channel的名称完全一致,两端的字符串必须匹配,否则通信会失败。
本质上就是把iOS的实现逻辑平移到macOS工程里,因为两者的Flutter引擎绑定方式几乎完全相同,官方文档虽然没单独列macOS,但底层原理是通用的。
内容的提问来源于stack exchange,提问作者Adrian M Cavazos
相关产品推荐
相关产品推荐

