当前位置: 首页 > news >正文

快递网站建设日程表简短的软文范例

快递网站建设日程表,简短的软文范例,wordpress登录缓存,网页设计基础实训报告flutter开发实战-MethodChannel实现flutter与iOS双向通信 最近开发中需要iOS与flutter实现通信,这里使用的MethodChannel 如果需要flutter与Android实现双向通信,请看 https://blog.csdn.net/gloryFlow/article/details/132218837 这部分与https://bl…

flutter开发实战-MethodChannel实现flutter与iOS双向通信

最近开发中需要iOS与flutter实现通信,这里使用的MethodChannel

如果需要flutter与Android实现双向通信,请看
https://blog.csdn.net/gloryFlow/article/details/132218837

这部分与https://blog.csdn.net/gloryFlow/article/details/132218837中的一致,这里实现一下iOS端的MethodChannel设置。

一、MethodChannel

MethodChannel:用于传递方法调用(method invocation)。
通道的客户端和宿主端通过传递给通道构造函数的通道名称进行连接

一个应用中所使用的所有通道名称必须是唯一的
使用唯一的域前缀为通道名称添加前缀,比如:samples.flutter.dev/battery

官网 https://flutter.cn/docs/development/platform-integration/platform-channels

二、在flutter端实现MethodChannel

我们需要创建一个名字为"samples.flutter.dev/test"的通道名称。
通过invokeNativeMethod与setMethodCallHandler来实现

invokeNativeMethod:调用Android端的代码
setMethodCallHandler:设置方法回调,用于接收Android端的参数

代码如下

import 'package:flutter/services.dart';//MethodChannel
const methodChannel = const MethodChannel('samples.flutter.dev/test');class FlutterMethodChannel {/** MethodChannel* 在方法通道上调用方法invokeMethod* methodName 方法名称* params 发送给原生的参数* return数据 原生发给Flutter的参数*/static Future<Map> invokeNativeMethod(String methodName,[Map? params]) async {var res;try {if (params == null) {res = await methodChannel.invokeMethod('$methodName');} else {res = await methodChannel.invokeMethod('$methodName', params);}} catch (e) {res = {'Failed': e.toString()};}return res;}/** MethodChannel* 接收methodHandler* methodName 方法名称* params 发送给原生的参数* return数据 原生发给Flutter的参数*/static void methodHandlerListener(Future<dynamic> Function(MethodCall call)? handler) {methodChannel.setMethodCallHandler(handler);}
}

使用该MethodChannel,我们需要使用MethodChannel
使用代码如下

  void initState() {// TODO: implement initStatesuper.initState();setMethodHandle();}void setMethodHandle() {FlutterMethodChannel.methodHandlerListener((call) {print("methodHandlerListener call:${call.toString()}");if ("methodToFlutter" == call.method) {print("methodToFlutter arg:${call.arguments}");}return Future.value("message from flutter");});}Future<void> invokeNativeMethod() async {var result = await FlutterMethodChannel.invokeNativeMethod("methodTest", {"param":"params from flutter"});print("invokeNativeMethod result:${result.toString()}");}void testButtonTouched() {invokeNativeMethod();}void dispose() {// TODO: implement disposesuper.dispose();}

这里我们处理了方法methodToFlutter来接收iOS端的传参数调用,同时处理后我们将结果"message from flutter"返回给iOS端。
我们调用iOS端的方法methodTest,并且传参,获取iOS端传回的结果。

三、在iOS端实现MethodChannel

在iOS中,同样我们实现了MethodChannel。
iOS实现MethodChannel需要实现FlutterPlugin,实现registerWithRegistrar
我这里命名一个SDFlutterMethodChannelPlugin继承NSObject,通过实现registerWithRegistrar方法

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {FlutterMethodChannel *methodChannel = [FlutterMethodChannel methodChannelWithName:kFlutterMethodChannelName binaryMessenger:[registrar messenger] codec:[FlutterStandardMethodCodec sharedInstance]];SDFlutterMethodChannelPlugin *instance = [[SDFlutterMethodChannelPlugin alloc] initWithMethodChannel:methodChannel];// 将插件注册为来自Dart端的传入方法调用的接收者 在指定的“ FlutterMethodChannel”上。[registrar addMethodCallDelegate:instance channel:methodChannel];
}

同样在插件SDFlutterMethodChannelPlugin中设置setMethodCallHandler及调用Flutter的方法

例如

__weak typeof(self) weakSelf = self;[self.methodChannel setMethodCallHandler:^(FlutterMethodCall *call, FlutterResult result) {[weakSelf handleMethodCall:call result:result];}];

通过handleMethodCall可以处理方法methodTest处理接收来自flutter的参数,处理后并将结果返回给flutter。

整体代码如下

SDFlutterMethodChannelPlugin.h

#import <Foundation/Foundation.h>
#import <Flutter/Flutter.h>@class SDFlutterMethodChannelPlugin;typedef void (^SDFlutterMethodChannelPluginCompletionBlock)(SDFlutterMethodChannelPlugin *plugin);@interface SDFlutterMethodChannelPlugin : NSObject<FlutterPlugin>- (instancetype)initWithMethodChannel:(FlutterMethodChannel *)methodChannel;@end

SDFlutterMethodChannelPlugin.m

#define kFlutterMethodChannelName @"samples.flutter.dev/test"@interface SDFlutterMethodChannelPlugin ()<FlutterStreamHandler>@property (nonatomic, strong) FlutterMethodChannel *methodChannel;@property (nonatomic, strong) NSTimer *sendMessageTimer;@end@implementation SDFlutterMethodChannelPlugin- (instancetype)initWithMethodChannel:(FlutterMethodChannel *)methodChannel {self = [super init];if (self) {self.flutterBridgeConfig = [[DFFlutterBridgeConfig alloc] init];self.methodChannel = methodChannel;__weak typeof(self) weakSelf = self;[self.methodChannel setMethodCallHandler:^(FlutterMethodCall *call, FlutterResult result) {[weakSelf handleMethodCall:call result:result];}];[self startSendMessageTimer];}return self;
}+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {FlutterMethodChannel *methodChannel = [FlutterMethodChannel methodChannelWithName:kFlutterMethodChannelName binaryMessenger:[registrar messenger] codec:[FlutterStandardMethodCodec sharedInstance]];SDFlutterMethodChannelPlugin *instance = [[SDFlutterMethodChannelPlugin alloc] initWithMethodChannel:methodChannel];// 将插件注册为来自Dart端的传入方法调用的接收者 在指定的“ FlutterMethodChannel”上。[registrar addMethodCallDelegate:instance channel:methodChannel];
}#pragma mark - FlutterPlugin协议方法
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {NSLog(@"config handleMethodChannel callmethod:%@,params:%@,result:%@", call.method, call.arguments, result);// 没有处理,需要单独处理NSString *method=call.method;if ([method isEqualToString:@"methodTest"]) {NSLog(@"flutter 调用到了 ios test");NSMutableDictionary *dic = [NSMutableDictionary dictionary];[dic setObject:@"result.success 返回给flutter的数据" forKey:@"message"];[dic setObject: [NSNumber numberWithInt:200] forKey:@"code"];result(dic);} else if ([method isEqualToString:@"test2"]) {NSLog(@"flutter 调用到了 ios test2");result(@YES);} else {result(FlutterMethodNotImplemented);}
}#pragma mark - 开启定时器
- (void)sendMessageTimerAction {// 开启[self.methodChannel invokeMethod:@"methodToFlutter" arguments:@"Params from Android"];
}/**开启定时器
*/
- (void)startSendMessageTimer {if (_sendMessageTimer) {return;}//开始其实就是开始定时器_sendMessageTimer = [NSTimer timerWithTimeInterval:6 target:self selector:@selector(sendMessageTimerAction) userInfo:nil repeats:YES];//加到runloop[[NSRunLoop currentRunLoop] addTimer:_sendMessageTimer forMode:NSRunLoopCommonModes];
}/**结束定时器*/
- (void)stopSendMessageTimer {//暂停其实就是销毁计时器[_sendMessageTimer invalidate];_sendMessageTimer = nil;
}@end

在iOS中需要在AppDelegate中设置,在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中实现

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {[SDFlutterMethodChannelPlugin registerWithRegistrar:[(id)[SDWeakProxy proxyWithTarget:self] registrarForPlugin:@"SDFlutterMethodChannelPlugin"]];return YES;
}

我们在iOS代码中实现MethodChanel,通过定时器NSTimer定时调用方法methodToFlutter将参数传递给Flutter端。通过在iOS端setMethodCallHandler根据方法methodTest处理接收来自flutter的参数,处理后并将结果返回给flutter。

四、小结

flutter开发实战-MethodChannel实现flutter与iOS双向通信。实现MethodChannel在flutter端与iOS端实现相互通信功能。

https://blog.csdn.net/gloryFlow/article/details/132240415

学习记录,每天不停进步。

http://www.hengruixuexiao.com/news/54389.html

相关文章:

  • 浙江华企做的网站怎么样汕头seo公司
  • 徐州网站建设网站制作爱站网站长工具
  • 南京做网站多少钱百度知道推广软件
  • 做视频课程网站国外比较开放的社交软件
  • 网站页面划分谷歌seo站内优化
  • 中国苏州网站软件开发公司推荐
  • 昭通市公安局网站是谁做的湖口网站建设
  • 红色网站主题怎样免费推广自己的网站
  • 新企业网站应该怎么做SEO优化近期网络营销的热点事件
  • 关于网站建设新闻宁德市市长
  • 公安机关网站备案怎么做找推网
  • 绵阳网站建设怎么选互动营销的案例及分析
  • 网站排名提升工具宣传网站站点最有效的方式是
  • 如何为网站做推广百度seo推广
  • 食用油 网站 模板万网官网域名查询
  • 做做网站下载2023宁德市教育局
  • 怎样找人做网站优化培训内容
  • 在线优化网站建设万网域名管理入口
  • 沙朗镇做网站公司sem搜索引擎营销是什么
  • 经常投诉网站快照seo优化网站网页教学
  • 哈尔滨哪里做网站好cba最新积分榜
  • 网站如何带来流量办理培训机构需要具备的条件
  • 网站建设及管理使用情况汇报百度推广一天烧多少钱
  • 淘宝直接怎么做网站互联网推广怎么做
  • 动易会提示模版文件"默认网站首页问答模板.html"找不到免费建立个人网站
  • 西安企业建站排名网店推广
  • 上海网站上海专业seo
  • 求网站老司机优化方案丛书官网
  • 建网站需要什么程序企业培训课程ppt
  • 性用品微商做的最好的网站唯尚广告联盟app下载