当前位置:   article > 正文

Flutter和iOS原生通信_flutter ios fluttereventsink

flutter ios fluttereventsink

1、iOS给Flutter传值

iOS代码:

添加FlutterEventChannel和eventChannel的属性,实列化FlutterEventChannel,设置binaryMessenger为当前flutterVC;

  1. @property (nonatomic, strong) FlutterEventChannel *eventChannel;
  2. @property (nonatomic, strong) FlutterEventSink eventSink;
  3. self.eventChannel = [FlutterEventChannel eventChannelWithName:@"App/Event/Channel" binaryMessenger:self.flutterVC];
  4. [self.eventChannel setStreamHandler:self];

设置FlutterStreamHandler的回调,self.eventSink = events;

  1. // 这个onListen是Flutter端开始监听这个channel时的回调,第二个参数 EventSink是用来传数据的载体。
  2. - (FlutterError* _Nullable)onListenWithArguments:(id _Nullable)arguments
  3. eventSink:(FlutterEventSink)events {
  4. self.eventSink = events;
  5. return nil;
  6. }
  7. /// flutter不再接收
  8. - (FlutterError* _Nullable)onCancelWithArguments:(id _Nullable)arguments {
  9. // arguments flutter给native的参数
  10. return nil;
  11. }

在需要发事件的地方设置参数

  1. NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
  2. [dic setSafeValue:@"YES" forKey:@"logout"];
  3. self.eventSink(dic);

Flutter代码:

  1. class _testPageState extends State {
  2. static const EventChannel eventChannel =
  3. const EventChannel('App/Event/Channel');//App/Event/Channel和iOS的一致
  4. @override
  5. void initState() {
  6. super.initState();
  7. //开始监听
  8. eventChannel
  9. .receiveBroadcastStream("init")
  10. .listen(_onEvent, onError: _onError);
  11. }
  12. // 数据接收
  13. void _onEvent(Object value) {
  14. print('onEvent+$value');
  15. }
  16. // 错误处理
  17. void _onError(dynamic) {}
  18. }

2、Flutter给iOS传值

iOS代码:带返回值和不带返回值

  1. @property (nonatomic, strong) FlutterMethodChannel *eventChannel;
  2. [self.eventChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
  3. @strongify(self);
  4. NSString *method = call.method;
  5. NSLog(@">>>>>>>eventChannel method:%@, arguments:%@", method, call.arguments);
  6. if ([method isEqualToString:@"commonData"]) {
  7. NSArray *languageList = [self getLanguageInfo];
  8. NSDictionary *dic = @{
  9. @"languages":HB_ARRAY(languageList),
  10. };
  11. NSString *jsonString = [self jsonString:dic];
  12. result(jsonString);
  13. }
  14. else if ([method isEqualToString:@"logOut"]) {
  15. [self logout];
  16. result(nil);
  17. }
  18. else {
  19. result(FlutterMethodNotImplemented);
  20. }
  21. }];
  22. }

Flutter代码:

  1. Future initCommonData() async {
  2. try {
  3. String data =
  4. await _channelMethod..invokeMethod(‘commonData');
  5. final parseJson = json.decode(data);
  6. ///原生获取所有数据数据
  7. SettingCommonData settingCommonData =
  8. SettingCommonData.fromJson(parseJson);
  9. print('SettngCommonData = $data');
  10. } on PlatformException catch (e) {
  11. print('PlatformException: ${e.toString()}');
  12. } on MissingPluginException catch (e) {
  13. print('MissingPluginException: ${e.toString()}');
  14. } catch (e) {
  15. print('other exception : ${e.toString()}');
  16. }
  17. ///退出
  18. logOut() async {
  19. try {
  20. await _channelMethod.invokeMethod('logOut');
  21. } on PlatformException catch (e) {
  22. print('PlatformException: ${e.toString()}');
  23. return '';
  24. } on MissingPluginException catch (e) {
  25. print('MissingPluginException: ${e.toString()}');
  26. return '';
  27. } catch (e) {
  28. print('other exception : ${e.toString()}');
  29. }
  30. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/383262
推荐阅读
相关标签
  

闽ICP备14008679号