flutter 代码块
//声明关联插件 '*********' 接收值ID
static const platform = MethodChannel('*********');
//在APP显示的view
Widget platformView() {
if (Platform.isAndroid) {
return AndroidView
//接受view的id
viewType: "android_view"
//带值到原生
creationParams: {
"key": value
},
creationParamsCodec: const StandardMessageCodec(),
);
} else if (Platform.isIOS) {
return UiKitView(
//接受view的id
viewType: 'iOS_view',
//带值到原生
creationParams: {
'key': value,
},
creationParamsCodec: StandardMessageCodec(),
);
}
}
swift
class FlutterViewFactory: NSObject, FlutterPlatformViewFactory {
var messenger:FlutterBinaryMessenger
//多层嵌套时 进行区分
var factorType = String()
init(messenger:FlutterBinaryMessenger, factorType: String) {
self.messenger = messenger
self.factorType = factorType
super.init()
}
func create(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?) -> FlutterPlatformView {
return FlutterNestingView(frame,viewID: viewId,args: args,messenger: messenger)
}
func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
return FlutterStandardMessageCodec.sharedInstance()
}
}
AppDelegate.swift
//注册嵌套的view
let registrar:FlutterPluginRegistrar = self.registrar(forPlugin: "iOS_view")!
let factory = FlutterViewFactory(messenger: registrar.messenger(), factorType: "factorType")
registrar.register(factory, withId: "iOS_view")
import SwiftyJSON
class FlutterNestingView: NSObject, FlutterPlatformView {
var methodChannel = FlutterMethodChannel()
init(_ frame: CGRect,viewID: Int64,args :Any?, messenger :FlutterBinaryMessenger) {
super.init()
//flutter声明时带过来的参数
if(args is NSDictionary){
let dict = JSON(args as Any).dictionaryValue
}
methodChannel = FlutterMethodChannel(name: '*********', binaryMessenger: messenger)
methodChannel.setMethodCallHandler { (call, result) in
}
}
func view() -> UIView {
return flutterView
}
}
传值 原生 to flutter
//----swift----
self.methodChannel.invokeMethod("methodName", arguments: mdic) { (result) in
print("结果")
}
//----flutter----
platform.setMethodCallHandler(receiveDataForPrimitive);
//接受原生返的值
Future<dynamic> receiveDataForPrimitive(MethodCall methodCall) async {
switch (methodCall.method) {
case "methodName":
Map<String, dynamic> map = new Map<String, dynamic>.from(methodCall.arguments);
print(map);
break;
}
}
传值 flutte to 原生
//flutter 传值
Future<void> _communicateFunction() async {
try {
//原生方法名为callNativeMethond,flutterPara为flutter调用原生方法传入的参数,await等待方法执行
await platform.invokeMethod( 'dispose', "value");
} on PlatformException catch (e) {
//抛出异常
//flutter: PlatformException(001, 进入异常处理, 进入flutter的trycatch方法的catch方法)
print(e);
}
}
//swift 接收传值
methodChannel.setMethodCallHandler { (call, result) in
if (call.method == "dispose"){
//输出value
print(result)
}
}
写在后面
StandardMessageCodec:BasicMessageChannel的默认编解码器,其支持基础数据类型、二进制数据、列表、字典,其工作原理会在下文中详细介绍。
StringCodec:用于字符串与二进制数据之间的编解码,其编码格式为UTF-8。
JSONMessageCodec:用于基础数据与二进制数据之间的编解码,其支持基础数据类型以及列表、字典。其在iOS端使用了NSJSONSerialization作为序列化的工具,而在Android端则使用了其自定义的JSONUtil与StringCodec作为序列化工具。
BinaryCodec:是最为简单的一种Codec,因为其返回值类型和入参的类型相同,均为二进制格式(Android中为ByteBuffer,iOS中为NSData)。实际上,BinaryCodec在编解码过程中什么都没做,只是原封不动将二进制数据消息返回而已。或许你会因此觉得BinaryCodec没有意义,但是在某些情况下它非常有用,比如使用BinaryCodec可以使传递内存数据块时在编解码阶段免于内存拷贝。