在混合開發(fā)中避免不了通信,簡(jiǎn)單記錄一下,F(xiàn)lutter iOS工程與Flutter 之間相互通信。Flutter
中通過Platform Channel實(shí)現(xiàn)Flutter和原生端的數(shù)據(jù)傳遞,是怎么進(jìn)行數(shù)據(jù)通信,以及怎么配置,下面一一進(jìn)行詳解。
- 通過Platform channel 將傳遞的數(shù)據(jù)以發(fā)送消息的形式。
- 跨線程發(fā)送到iOS端和Android 原生端收到Platform
Channel傳遞過來的消息后,進(jìn)行相應(yīng)處理 。
原生端處理完畢,將結(jié)果以同樣的方式原路返回給應(yīng)用對(duì)應(yīng)的Flutter部分。
FlutterMethodChannel
使用
注:iOS 端簡(jiǎn)單設(shè)置
class HYFlutterNavChannel: NSObject {
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)
}
}
public static func start() {
_ = HYFlutterNavChannel.share
}
// pop
func pop(call: FlutterMethodCall, result: FlutterResult) {
UINavigationController.topNavigationController()?.navigationController?.popViewController(animated: true)
}
}
在iOS 注冊(cè)Flutter 引擎的地方使用
// 案例是放到 AppDelegate中
[FlutterBoost.instance setup:application delegate:delegate callback:^(FlutterEngine *engine) {
NSLog(@"FlutterBoost 開始操作");
// 使用 MethodChannel
[HYFlutterNavChannel start];
[HYFlutterCommonChannel start];
}];
上述就把iOS端,使用FlutterMethodChannel
簡(jiǎn)單進(jìn)行通信集成完畢。
- Flutter 端
MethodChannel
集成與使用
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
這里使用了一個(gè)類進(jìn)行統(tǒng)一管理 通信類
import 'package:my_flutter/common_channel.dart';
import 'navigation_channel.dart';
class Channels {
// ignore: empty_constructor_bodies
Channels._() {}
// 注冊(cè) Channel
static final navigation = NavigationChannel();
static final common = CommonChannel();
}
在Flutter使用的地方進(jìn)行調(diào)用文章來源:http://www.zghlxwxcb.cn/news/detail-636181.html
Channels.navigation.finishHostPage();
上述完成,flutter就可以調(diào)用原生里面注冊(cè)的pop方法了。文章來源地址http://www.zghlxwxcb.cn/news/detail-636181.html
到了這里,關(guān)于Flutter iOS 與 flutter 相互通信的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!