1. Native communication of Flutter and iOS

1.1. Upload handle to Flutter

  • Create handle to Flutter:
    MethodChannel _methodChannel = MethodChannel('channelKey');
    Copy the code
  • Send handle content 🙁The arguments are optional)
    _methodChannel.invokeMapMethod('sendIOSKey');
    Copy the code

1.2. Handle to Flutter receive

  • Receives handle content
    _methodChannel.setChannelCallHandler((call){ if(call.method == 'sendFlutterKey') { print(call.arguments); }});Copy the code

1.3. IOS sends handle

  • IOS create handle:MethodChannelWithName: binaryMessenger:
    FlutterMethodChannel *methodChannel = [FlutterMethodChannel MethodChannelWithName:'channelKey' binaryMessenger:flutterVC.binaryMessenger];
    Copy the code
  • Send handle contents:
    [methodChannel invokeMethod:"sendFlutterKey" arguments: send content];Copy the code

1.4. IOS Receive handle

  • Receive handle contents:
    [methodChannel setMethodCallHandler:^(FlutterMethodCall * call,FlutterResult result){
        if([call.method isEqualToString:'sendIOSKey']){
        }
    }];
    Copy the code

Note:

  • A channel is created between the Flutter and iOS, and a connection is established when the key name is paired
  • The arguments for Flutter send handle content are optional, while the arguments for iOS send handle content are explicit
  • Receiving Channel callscall, the parameters for.methodand.arguments

2. IOS projects embed the Flutter page

2.1. Podfile configuration

2.2,setInitialRoute:(Flutter is not recommended, memory explosion)

2.2.1 Launch the Flutter page in iOS
  • [flutterVC setInitialRoute:”key”];
  • window.defaultRouteNameMyApp() of Flutter is called by iOSRouteParameters’ key ‘
2.2.2 Callback iOS after the Flutter operation
  • Operate using the Flutter engine
  • throughinitWithEngine:Create flutterVC,
    • The shortcoming cannot transmit parameters to Flutter, so thatsetInitialRoute:failure
    • Advantages Memory does not burst

3. Three types of Channel

3.1, MethodChannel

  • Pass method call

3.2, BasicMessageChannel

  • Continuous communication, and the ability to respond to messages when received
  • FlutterCreate BasicMessageChannel:BasicMessageChannel _msgChannel = BasicMessageChannel('channelKey',StandardMessageCodec());
    • StandardMessageCodec(): codec, required
    • Content to send:_msgchannel. send(send content);
  • iOSCreate BasicMessageChannel:BasicMessageChannel _msgChannel = [FlutterBasicChannel messageChannelWithName:"channelKey" binaryMessenger:_flutterVC.binaryMessenger];
    • Content to send:[_msgChannel sendMessage: send content];

3.3, the EventChannel

  • The data flow
Note:
  1. Flutter code modifications are sometimes requiredcommand+shift+kClean up the cache run to see the changes, sometimes you need to run in AS first
  2. IOS creates the Flutter engine (a Flutter interface with no hierarchy)