Flutter iOS 与 flutter 相互通信

在混合开发中避免不了通信,简单记录一下,Flutter iOS工程与Flutter 之间相互通信。
Flutter中通过Platform Channel实现Flutter和原生端的数据传递,是怎么进行数据通信,以及怎么配置,下面一一进行详解。

  • FlutterMethodChannel 使用
    注:iOS 端简单设置
dart 复制代码
class HYFlutterNavChannel: NSObject {
    
    @objc public static let share = HYFlutterNavChannel()
    // 声明 FlutterMethodChannel
    var channel: FlutterMethodChannel
    // 
    lazy var map: [String: (_ call: FlutterMethodCall, _ result: FlutterResult) -> Void] = {
        return [
            "pop":pop,
        ]
    }()
    
    
    override init() {
        // name 一定需要和  flutter里面约定好,保持一致
        channel = FlutterMethodChannel.init(name: "Flutter/navigation", binaryMessenger: FlutterBoost.instance().engine().binaryMessenger)
        
        super.init()
        
        channel.setMethodCallHandler {[weak self] (call, reslt) in
            let method = self?.map[call.method]
            method?(call, reslt)
        }
        
    }
    
    @objc public static func start() {
        _ = HYFlutterNavChannel.share
    }
    
    
    // pop
    func pop(call: FlutterMethodCall, result: FlutterResult)  {
        UINavigationController.topNavigationController()?.navigationController?.popViewController(animated: true)
    }
    
}

在iOS 注册Flutter 引擎的地方使用

dart 复制代码
// 案例是放到 AppDelegate中
[FlutterBoost.instance setup:application delegate:delegate callback:^(FlutterEngine *engine) {
        NSLog(@"FlutterBoost 开始操作");
        // 使用 MethodChannel
        [HYFlutterNavChannel start];
        [HYFlutterCommonChannel start];
    }];

上述就把iOS端,使用FlutterMethodChannel简单进行通信集成完毕。

  • Flutter 端 MethodChannel 集成与使用
bash 复制代码
import 'dart:collection';

import 'package:flutter/services.dart';

class NavigationChannel {
  // 这里需要和原生保存一致  "Flutter/navigation"
  // ignore: constant_identifier_names
  static const MethodChannel channel_navigation =
      MethodChannel("Flutter/navigation");

  // ignore: non_constant_identifier_names
  static final channel_navigation_handlers =
      HashMap<String, MethodCallHandler>();

  NavigationChannel() {
    init();
  }

  void init() {
    channel_navigation_handlers["nativeQuitFlutter"] = nativeQuitFlutter;
    channel_navigation.setMethodCallHandler((call) async {
      channel_navigation_handlers[call.method]?.call(call);
    });
  }

  //  native 提供的功能方法

  Future<void> finishHostPage() async {
    return channel_navigation.invokeMethod("pop");
  }

  Future<void> nativeQuitFlutter(MethodCall call) async {}

// -------------flutter提供的功能-----------------
  void registerInitRoute(MethodCallHandler handler) {
    channel_navigation_handlers["initRoute"] = handler;
  }
}

typedef MethodCallHandler = Future<dynamic> Function(MethodCall call)?;

以上 Flutter MethodChannel 集成完毕

  • Flutter 使用MethodChannel
    这里使用了一个类进行统一管理 通信类
bash 复制代码
import 'package:my_flutter/common_channel.dart';

import 'navigation_channel.dart';

class Channels {
  // ignore: empty_constructor_bodies
  Channels._() {}

// 注册 Channel
  static final navigation = NavigationChannel();
  static final common = CommonChannel();
}

在Flutter使用的地方进行调用

bash 复制代码
Channels.navigation.finishHostPage();

上述完成,flutter就可以调用原生里面注册的pop方法了。

相关推荐
2501_9151063212 分钟前
不依赖 Xcode 的 iOS 编译器,kxapp 中 kxbuild 工具详解
ide·vscode·ios·cocoa·个人开发·xcode·敏捷流程
2501_9206276129 分钟前
Flutter 框架跨平台鸿蒙开发 - 数学学习助手
学习·flutter·华为·harmonyos
2501_9206276139 分钟前
Flutter 框架跨平台鸿蒙开发 - 计算器增强版
flutter·华为·harmonyos
2501_9206276141 分钟前
Flutter 框架跨平台鸿蒙开发 - 单词卡记忆
flutter·华为·harmonyos
芙莉莲教你写代码1 小时前
Flutter 框架跨平台鸿蒙开发 - 宝石消除游戏
flutter·游戏·华为·harmonyos
louisgeek12 小时前
Flutter autoDispose、keepAlive 和 ref.keepAlive 的区别
flutter
疯狂的程序猴15 小时前
iOS 多技术栈混淆实现,跨平台 App 混淆拆解与组合
后端·ios
左手厨刀右手茼蒿16 小时前
Flutter 三方库 firebase_admin 跨云边管线企业级鸿蒙管控底座适配风云:无障碍贯穿服务器授权防火墙打通底层生态授权域并构建海量设备推送集结-适配鸿蒙 HarmonyOS ohos
服务器·flutter·harmonyos
钛态16 小时前
Flutter for OpenHarmony:shelf_web_socket 快速构建 WebSocket 服务端,实现端到端实时通信(WebSocket 服务器) 深度解析与鸿蒙适配指南
服务器·前端·websocket·flutter·华为·性能优化·harmonyos
亚历克斯神16 小时前
Flutter 三方库 at_server_status 的鸿蒙化适配指南 - 在鸿蒙系统上构建极致、透明、实时的 @protocol 去中心化身份服务器状态感知与鉴权监控引擎
flutter·华为·harmonyos