Flutter和iOS原生通信

1、iOS给Flutter传值

iOS代码:

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

@property (nonatomic, strong) FlutterEventChannel *eventChannel;
@property (nonatomic, strong) FlutterEventSink eventSink;

 self.eventChannel = [FlutterEventChannel eventChannelWithName:@"App/Event/Channel" binaryMessenger:self.flutterVC];
 [self.eventChannel setStreamHandler:self];

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

// 这个onListen是Flutter端开始监听这个channel时的回调,第二个参数 EventSink是用来传数据的载体。
- (FlutterError* _Nullable)onListenWithArguments:(id _Nullable)arguments
                                       eventSink:(FlutterEventSink)events {
    self.eventSink = events;
    return nil;
}

/// flutter不再接收
- (FlutterError* _Nullable)onCancelWithArguments:(id _Nullable)arguments {
    // arguments flutter给native的参数
    return nil;
}

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

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

Flutter代码:

class _testPageState extends State {
  static const EventChannel eventChannel =
      const EventChannel('App/Event/Channel');//App/Event/Channel和iOS的一致

  @override
  void initState() {
    super.initState();
    //开始监听
    eventChannel
        .receiveBroadcastStream("init")
        .listen(_onEvent, onError: _onError);
  }

// 数据接收
  void _onEvent(Object value) {
    print('onEvent+$value');
  }

// 错误处理
  void _onError(dynamic) {}
}

2、Flutter给iOS传值

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

@property (nonatomic, strong) FlutterMethodChannel *eventChannel;

  [self.eventChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
        @strongify(self);
        NSString *method = call.method;
        NSLog(@">>>>>>>eventChannel method:%@, arguments:%@", method, call.arguments);
        if ([method isEqualToString:@"commonData"]) {
            NSArray *languageList = [self getLanguageInfo];
            NSDictionary *dic = @{
                @"languages":HB_ARRAY(languageList),
            };
            NSString *jsonString = [self jsonString:dic];
            result(jsonString);
        }
        else if ([method isEqualToString:@"logOut"]) {
            [self logout];
            result(nil);
        }
        else {
            result(FlutterMethodNotImplemented);
        }
    }];
}

Flutter代码:

Future initCommonData() async {
  try {
    String data =
        await _channelMethod..invokeMethod(‘commonData');
    final parseJson = json.decode(data);

    ///原生获取所有数据数据
    SettingCommonData settingCommonData =
        SettingCommonData.fromJson(parseJson);
    print('SettngCommonData = $data');
} on PlatformException catch (e) {
  print('PlatformException: ${e.toString()}');
} on MissingPluginException catch (e) {
  print('MissingPluginException: ${e.toString()}');
} catch (e) {
  print('other exception : ${e.toString()}');
}

///退出
logOut() async {
  try {
    await _channelMethod.invokeMethod('logOut');
  } on PlatformException catch (e) {
    print('PlatformException: ${e.toString()}');
    return '';
  } on MissingPluginException catch (e) {
    print('MissingPluginException: ${e.toString()}');
    return '';
  } catch (e) {
    print('other exception : ${e.toString()}');
  }
}

 

你可能感兴趣的:(Flutter)