美文网首页
Flutter 与 Native 的交互

Flutter 与 Native 的交互

作者: 贝灬小晖 | 来源:发表于2020-06-16 16:39 被阅读0次

现阶段只做了信息传输

使用的是 BasicMessageChannel

iOS 端

 let flutterVC = self.window.rootViewController
        messageChannel = FlutterBasicMessageChannel.init(name: "flutter_and_native_100", binaryMessenger: flutterVC as! FlutterBinaryMessenger)
        // 接收消息监听

        messageChannel?.setMessageHandler({ (message, callback) in
           //根据不同message ,callback不同的信息
            callback("1000")
        })

该方法写在中

override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ){
}

flutter端

static const messageChannel = const BasicMessageChannel('flutter_and_native_100', StandardMessageCodec());


Future<String> sendMessage() async {
    String reply = await messageChannel.send('callNative');
    print('reply: $reply');
    return reply;
}

在需要调用的地方执行 sendMessage 以获取reply内容

使用 MethodChannel

iOS端

let flutterVC = self.window.rootViewController
        messageChannel = FlutterMethodChannel.init(name: "com.weteam/pushToken", binaryMessenger: flutterVC as! FlutterBinaryMessenger)
        messageChannel?.setMethodCallHandler({ (message, callback) in
            callback("1000")
        })

可以根据不同的消息callback不同的信息

该方法写在中

override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ){
}

flutter 端

static const platform = const MethodChannel('com.weteam/pushToken');

Future<String> sendMessage() async {
    String reply = await platform.invokeMethod('getPushToken');
    print('reply: $reply');
    return reply;
}

相关文章

网友评论

      本文标题:Flutter 与 Native 的交互

      本文链接:https://www.haomeiwen.com/subject/gisrxktx.html