Flutter开发之——IOS通信MethodChannel

一 概述

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

二 MethodChannel通信示例

2.1 创建Flutter端MethodChannel

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

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

class _MethodChannelDemoState extends State<MethodChannelDemo> {
  var channel = MethodChannel('com.example.flutterios.MethodChannel');

  var _data;

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

说明:com.example.flutterios.MethodChannel是iOS端MethodChannel全路径

2.2 创建IOS端MethodChannelDemo

import Flutter
import UIKit

public class MethodChannelDemo {
    
    init(messenger: FlutterBinaryMessenger) {
        let channel = FlutterMethodChannel(name: "com.example.flutterios.MethodChannel", binaryMessenger: messenger)
        channel.setMethodCallHandler { (call:FlutterMethodCall, result:@escaping FlutterResult) in
            if (call.method == "sendData") {
                if let dict = call.arguments as? Dictionary<String, Any> {
                    let name:String = dict["name"] as? String ?? ""
                    let age:Int = dict["age"] as? Int ?? -1
                    result(["name":"hello,\(name)","age":age])
                }
            }
        }
    }
}

2.3 在AppDelegate设置MethodChannelDemo

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    
    GeneratedPluginRegistrant.register(with: self)
    let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
    MethodChannelDemo(messenger: controller.binaryMessenger)
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

2.4 效果图

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

3.1 创建Flutter端MethodChannel

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

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

class _MethodChannelDemoState extends State<MethodChannelDemo> {
  var channel = MethodChannel('com.example.flutterios.MethodChannel');
  var _data;
  var _nativeData;

 @override
  void initState() {
   channel.setMethodCallHandler((call)async{
     setState(() {
       _nativeData = call.arguments['count'];
     });
   });
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text("IOS-MethodChannelDemo")),
        body: Column(
          children: [
            SizedBox(height: 50,),
            RaisedButton(
              child: Text('发送数据到原生'),
              onPressed: () async {
                var result = await channel
                    .invokeMethod('sendData', {'name': '张三', 'age': 18});
                var name = result['name'];
                var age = result['age'];
                setState(() {
                  _data = '$name,$age';
                });
              },
            ),
            Text('原生返回数据:$_data'),
            Text('原生主动发送数据:$_nativeData')
          ],
        ),
      ),
    );
  }
}

3.2 创建IOS端MethodChannelDemo

import Flutter
import UIKit

public class MethodChannelDemo {
    var count =  0
    var channel:FlutterMethodChannel
    init(messenger: FlutterBinaryMessenger) {
        channel = FlutterMethodChannel(name: "com.example.flutterios.MethodChannel", binaryMessenger: messenger)
        channel.setMethodCallHandler { (call:FlutterMethodCall, result:@escaping FlutterResult) in
            if (call.method == "sendData") {
                if let dict = call.arguments as? Dictionary<String, Any> {
                    let name:String = dict["name"] as? String ?? ""
                    let age:Int = dict["age"] as? Int ?? -1
                    result(["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.invokeMethod("timer", arguments:args)
    }
}

3.3 在合适的地方调用MethodChannel

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    
    GeneratedPluginRegistrant.register(with: self)
    let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
    MethodChannelDemo(messenger: controller.binaryMessenger)
    GeneratedPluginRegistrant.register(with: self)
       
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

3.4 效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值