Flutter与iOS原生通信方式

简介: Flutter与iOS原生通信方式

三种主要通信方式

  • MethodChannel:与原生相互进行方法调用,用于方法调用(双向)
  • BasicMessageChannel:与原生相互发送消息,用于数据传输(双向)
  • EventChannel:原生发送消息,Flutter接收,用于数据流通信(单向)

具体实现

1. MethodChannel

使用方式以及交互流程,请查看截图中代码以及注释OC初始化相关代码

@interface ViewController ()<FlutterStreamHandler>
@property(strong, nonatomic) NSTimer *timer;
@property(strong, nonatomic) FlutterEngine *flutterEngine;
@property(strong, nonatomic) FlutterViewController *flutterVC;
@property(strong, nonatomic) FlutterBasicMessageChannel *basicMessageChannel;
@property(strong, nonatomic) FlutterEventChannel *eventChannel;
@property(copy, nonatomic) FlutterEventSink eventSink;
@end

OC懒加载相关代码

- (FlutterEngine *)flutterEngine {
    if (!_flutterEngine) {
        FlutterEngine *engine = [[FlutterEngine alloc] init];
        if (engine.run) {
            _flutterEngine = engine;
        }
    }
    return _flutterEngine;
}
- (FlutterViewController *)flutterVC {
    if (!_flutterVC) {
        _flutterVC = [[FlutterViewController alloc] initWithEngine:self.flutterEngine nibName:nil bundle:nil];
    }
    return _flutterVC;
}
- (FlutterBasicMessageChannel *)basicMessageChannel {
    if (!_basicMessageChannel) {
        //注册FlutterBasicMessageChannel - 并指定名称messageChannelName为basicMessageChannel
        _basicMessageChannel = [FlutterBasicMessageChannel messageChannelWithName:@"basicMessageChannel" binaryMessenger:self.flutterVC.binaryMessenger];
        //监听Flutter发来的消息 - Flutter调用OC回调监听
        [_basicMessageChannel setMessageHandler:^(id  _Nullable message, FlutterReply _Nonnull callback) {
            NSLog(@"OC收到Flutter的FlutterBasicMessageChannel - %@", message);
        }];
    }
    return _basicMessageChannel;
}
- (FlutterEventChannel *)eventChannel {
    if (!_eventChannel) {
        _eventChannel = [FlutterEventChannel eventChannelWithName:@"eventChannel" binaryMessenger:self.flutterVC.binaryMessenger];
        [_eventChannel setStreamHandler:self];
    }
    return _eventChannel;
}

2. BasicMessageChannel

使用方式以及交互流程,请查看截图中代码以及注释3. EventChannel使用方式以及交互流程,请查看截图中代码以及注释运行结果

flutter: 消息内容: OC通过EventChannel向Flutter发送消息(OC参数类型是id), Flutter收到消息数据类型: String

完整代码(github中Flt文件夹里面的内容):
https://github.com/gltwy/public

相关文章
|
1月前
|
开发框架 移动开发 Android开发
安卓与iOS开发中的跨平台解决方案:Flutter入门
【9月更文挑战第30天】在移动应用开发的广阔舞台上,安卓和iOS两大操作系统各自占据半壁江山。开发者们常常面临着选择:是专注于单一平台深耕细作,还是寻找一种能够横跨两大系统的开发方案?Flutter,作为一种新兴的跨平台UI工具包,正以其现代、响应式的特点赢得开发者的青睐。本文将带你一探究竟,从Flutter的基础概念到实战应用,深入浅出地介绍这一技术的魅力所在。
76 7
|
2月前
|
JSON Dart Java
flutter开发多端平台应用的探索 下 (跨模块、跨语言通信之平台通道)
flutter开发多端平台应用的探索 下 (跨模块、跨语言通信之平台通道)
|
3月前
|
XML 安全 Android开发
Flutter配置Android和IOS允许http访问
Flutter配置Android和IOS允许http访问
115 3
|
3月前
|
开发工具 iOS开发
解决Flutter运行报错Could not run build/ios/iphoneos/Runner.app
解决Flutter运行报错Could not run build/ios/iphoneos/Runner.app
140 2
|
3月前
|
iOS开发
解决Flutter运行IOS报错:Podfile is out of date
解决Flutter运行IOS报错:Podfile is out of date
74 1
|
3月前
|
iOS开发 Perl
IOS集成flutter_boost 3.0常见问题
IOS集成flutter_boost 3.0常见问题
69 0
|
3月前
|
开发框架 监控 iOS开发
Flutter IOS 打包上架踩坑
Flutter IOS 打包上架踩坑
107 0
|
4月前
|
安全 Shell Android开发
Flutter和Native 通信 pigeon
Flutter和Native 通信 pigeon
|
4月前
|
Dart Android开发 Windows
Flutter和Native 通信 android端
Flutter和Native 通信 android端