Flutter开发之——IOS通信BasicMessageChannel

一 概述

  • 创建Flutter端BasicMessageChannel
  • 创建IOS端BasicMessageChannel
  • 在合适的地方调用BasicMessageChannel

二 BasicMessageChannel通信示例

2.1 Flutter 端创建 MethodChannel 通道

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
runApp(BasicMessageChannelDemo());
}
class BasicMessageChannelDemo extends StatefulWidget {
@override
_BasicMessageChannelDemoState createState() => _BasicMessageChannelDemoState();
}

class _BasicMessageChannelDemoState extends State<BasicMessageChannelDemo> {
var channel = BasicMessageChannel('com.example.flutterios.BasicMessageChannel',StandardMessageCodec());
var _data;

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("BasicMessageChannelDemo"),),
body: Column(
children: [
SizedBox(
height: 50,
),
RaisedButton(
child: Text('发送数据到原生'),
onPressed: () async {
var result = await channel.send({'name': '张三', 'age': 18}) as Map;
var name = result['name'];
var age = result['age'];
setState(() {
_data = '$name,$age';
});
},
),
Text('原生返回数据:$_data'),
],
),
),
);
}
}

2.2 创建IOS端BasicMessageChannel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import Flutter
import UIKit

public class BasicMessageChannelDemo {

var channel:FlutterBasicMessageChannel

init(messenger: FlutterBinaryMessenger) {
channel = FlutterBasicMessageChannel(name: "com.example.flutterios.BasicMessageChannel", binaryMessenger: messenger)
channel.setMessageHandler { (message, reply) in
if let dict = message as? Dictionary<String, Any> {
let name:String = dict["name"] as? String ?? ""
let age:Int = dict["age"] as? Int ?? -1
reply(["name":"hello,\(name)","age":age])
}
}
}
}

2.3 在合适的地方调用BasicMessageChannel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {

let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
BasicMessageChannelDemo(messenger: controller.binaryMessenger)
GeneratedPluginRegistrant.register(with: self)

return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}

2.4 效果图

三 iOS 端向Flutter主动发送数据-定时器

3.1 Flutter 端创建 MethodChannel 通道

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
runApp(BasicMessageChannelDemo());
}
class BasicMessageChannelDemo extends StatefulWidget {
@override
_BasicMessageChannelDemoState createState() => _BasicMessageChannelDemoState();
}

class _BasicMessageChannelDemoState extends State<BasicMessageChannelDemo> {
var channel = BasicMessageChannel('com.example.flutterios.BasicMessageChannel',StandardMessageCodec());
var _nativeData;
var _data;
@override
void initState() {
channel.setMessageHandler((message) async{
setState(() {
_nativeData = (message as Map)['count'];
});
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("BasicMessageChannelDemo"),),
body: Column(
children: [
SizedBox(
height: 50,
),
RaisedButton(
child: Text('发送数据到原生'),
onPressed: () async {
var result = await channel.send({'name': '张三', 'age': 18}) as Map;
var name = result['name'];
var age = result['age'];
setState(() {
_data = '$name,$age';
});
},
),
Text('原生返回数据:$_data'),
Text('原生主动发送数据:$_nativeData'),
],
),
),
);
}
}

3.2 创建IOS端BasicMessageChannel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import Flutter
import UIKit

public class BasicMessageChannelDemo {

var channel:FlutterBasicMessageChannel
var count = 0

init(messenger: FlutterBinaryMessenger) {
channel = FlutterBasicMessageChannel(name: "com.example.flutterios.BasicMessageChannel", binaryMessenger: messenger)
channel.setMessageHandler { (message, reply) in
if let dict = message as? Dictionary<String, Any> {
let name:String = dict["name"] as? String ?? ""
let age:Int = dict["age"] as? Int ?? -1
reply(["name":"hello,\(name)","age":age])
}
}
startTimer()
}

func startTimer() {
var timer = Timer.scheduledTimer(timeInterval:1, target: self, selector:#selector(self.tickDown),userInfo:nil,repeats: true)
}
@objc func tickDown(){
count += 1
var args = ["count":count]
channel.sendMessage(args) { (reply) in

}
}
}

3.3 在合适的地方调用BasicMessageChannel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {

let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
BasicMessageChannelDemo(messenger: controller.binaryMessenger)
GeneratedPluginRegistrant.register(with: self)

return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}

3.4 效果图