前言:BasicMessageChannel是flutter官方的一个通讯手段,用于混合工程中flutter和原生的通讯。
用法:
iOS端:
FBFlutterViewContainer * fvc = [[FBFlutterViewContainer alloc]init];
[fvc setName:@"FollowingPage" uniqueId:nil params:nil opaque:true];
[weakSelf.navigationController pushViewController:fvc animated:YES];
[UIApplication sharedApplication].keyWindow.rootViewController;
FlutterBasicMessageChannel* messageChannel = [FlutterBasicMessageChannel messageChannelWithName:@"flutter_and_native_channel_name" binaryMessenger:fvc.binaryMessenger];
[messageChannel sendMessage:@{@"flutter messag":@"native向flutter发送了参数\n"}];
Fluttter端:
static const messageChannel =
BasicMessageChannel('flutter_and_native_channel_name', StandardMessageCodec());
channel.setMessageHandler((message) async{
// 处理接收到的消息
print('Received message: $message');
return " ";
});
其中flutter端的接收也可以放到main里,说明不一定是跳转过来的页才能接收消息。
网友评论