preface

There are three ways MethodChannel BasicMessageChannel, commonly used is the EventChannel MethodChannel, Native client calls need to perform in the main thread

OC

dispatch_async(dispatch_get_main_queue(), ^{
  
});
Copy the code

swift

DispatchQueue.main.async {
  
}
Copy the code

content

To embed iOS views into Flutter, you need to create two classes, as shown in the following example

Myflutterview. swift: Method of writing and Flutter communication

MyFlutterViewFactory. Swift: bind Id value

Appdelegate. swift: Registers the view

Bind a view type to a device type in A Flutter

static const platform = const MethodChannel('com.flutter.guide.MyFlutterView'); Var _data = 'get data '; / / call modified value in setState can update the UI display var platforms = []; // If the page has multiple native Spaces, you can use arrays or dictionaries to fetch and communicate... Widget platformView(){if (defaultTargetPlatform == targetPlatform.android){return AndroidView(//) viewType: 'plugins.flutter.io/custom_platform_view', // onPlatformViewCreated: (viewId) { // print('viewId:$viewId'); // platforms // .add(MethodChannel('com.flutter.guide.MyFlutterView_$viewId')); }, // creationParams: {'text': 'parameters to a Flutter to AndroidTextView '}, // creationParamsCodec: StandardMessageCodec(),); }else if(defaultTargetPlatform == TargetPlatform.iOS){ return UiKitView( viewType: 'plugins.flutter.io/custom_platform_view', onPlatformViewCreated: (viewId){ print('viewId:$viewId'); platforms.add(MethodChannel('com.flutter.guide.MyFlutterView_$viewId')); }, creationParams: {'text': 'parameters to Flutter to IOSTextView '},// pass the initialization parameter creationParamsCodec to iOS: StandardMessageCodec(),// encode creationParams and send it to the platform side); }}Copy the code

It passes the value to iOS when the view is initialized via the creationParams parameter in UiKitView

You can also pass parameters through the invokeMethod method in MethodChannel

After passing, it is also possible to parse and return data that is worth sending back

var result = await platforms[0].invokeMethod('getData', {'name': 'tqh', 'age': 18});
Copy the code

IOS uses the FlutterMethodChannel to get the view to respond to through name

let methodChannel = FlutterMethodChannel(name: "com.flutter.guide.MyFlutterView_\(viewID)", binaryMessenger: messenger)
Copy the code

The setMethodCallHandler method responds and sends back the data

methodChannel.setMethodCallHandler {(call, Result :FlutterResult) in // upload data to iOS if (call.method == "setText") {if let dict = call.arguments as? Dictionary<String, Any> { let name:String = dict["name"] as? String ?? "" let age:Int = dict["age"] as? Int ?? Self.label. text = "hello,\(name), age: Else if (call.method == "getData"){if let dict = call.arguments as? Dictionary<String, Any> { let name:String = dict["name"] as? String ?? "" let age:Int = dict["age"] as? Int ?? 1 result ([" name ":" I am a request after \ (name) ", "age" : the age])}}}Copy the code

code

Github.com/tqhnet/Flut…

Refer to the link

[Mixed development] Embedded native ios-View

[Mixed development] Communicate with native MethodChannel