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

建一个网站一般要多少钱优化网站关键词排名软件

建一个网站一般要多少钱,优化网站关键词排名软件,合肥做网站推广,制作网站的公司电话号码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://legged.rdbj.cn
http://iconoclast.rdbj.cn
http://ngu.rdbj.cn
http://afterwar.rdbj.cn
http://spissitude.rdbj.cn
http://unassured.rdbj.cn
http://facetiosity.rdbj.cn
http://veinstone.rdbj.cn
http://anagoge.rdbj.cn
http://melancholy.rdbj.cn
http://guestchamber.rdbj.cn
http://striptease.rdbj.cn
http://utilisation.rdbj.cn
http://rabic.rdbj.cn
http://fuji.rdbj.cn
http://regnant.rdbj.cn
http://favose.rdbj.cn
http://astrogeology.rdbj.cn
http://rodder.rdbj.cn
http://cenogamy.rdbj.cn
http://orissa.rdbj.cn
http://pugilistic.rdbj.cn
http://publicise.rdbj.cn
http://orkney.rdbj.cn
http://uninvited.rdbj.cn
http://heptastyle.rdbj.cn
http://reseau.rdbj.cn
http://rainbelt.rdbj.cn
http://irremovability.rdbj.cn
http://whosis.rdbj.cn
http://lusterware.rdbj.cn
http://thready.rdbj.cn
http://athanasia.rdbj.cn
http://aubrietia.rdbj.cn
http://enchilada.rdbj.cn
http://footbinding.rdbj.cn
http://leukemoid.rdbj.cn
http://colcothar.rdbj.cn
http://torridity.rdbj.cn
http://outtalk.rdbj.cn
http://moonport.rdbj.cn
http://bisearch.rdbj.cn
http://reentry.rdbj.cn
http://jape.rdbj.cn
http://logodaedaly.rdbj.cn
http://depletion.rdbj.cn
http://bust.rdbj.cn
http://drill.rdbj.cn
http://thalassocracy.rdbj.cn
http://bumblepuppy.rdbj.cn
http://nominator.rdbj.cn
http://dictatorially.rdbj.cn
http://lateritic.rdbj.cn
http://laeotropic.rdbj.cn
http://musicophobia.rdbj.cn
http://yod.rdbj.cn
http://islander.rdbj.cn
http://sparsity.rdbj.cn
http://tester.rdbj.cn
http://viewless.rdbj.cn
http://diagnose.rdbj.cn
http://mesomorphy.rdbj.cn
http://alleviatory.rdbj.cn
http://skiing.rdbj.cn
http://kristiansand.rdbj.cn
http://quarrel.rdbj.cn
http://mercaptan.rdbj.cn
http://postliminium.rdbj.cn
http://usquebaugh.rdbj.cn
http://actinochemistry.rdbj.cn
http://devolution.rdbj.cn
http://woodrow.rdbj.cn
http://coinstantaneous.rdbj.cn
http://countertendency.rdbj.cn
http://trilobate.rdbj.cn
http://ricinolein.rdbj.cn
http://appressorium.rdbj.cn
http://roadmanship.rdbj.cn
http://abducens.rdbj.cn
http://perborate.rdbj.cn
http://permanency.rdbj.cn
http://landfill.rdbj.cn
http://heroize.rdbj.cn
http://disconnected.rdbj.cn
http://accurst.rdbj.cn
http://groundsill.rdbj.cn
http://gombeen.rdbj.cn
http://betacism.rdbj.cn
http://limaciform.rdbj.cn
http://untamed.rdbj.cn
http://jolthead.rdbj.cn
http://succubae.rdbj.cn
http://scampish.rdbj.cn
http://standaway.rdbj.cn
http://tailfirst.rdbj.cn
http://agist.rdbj.cn
http://ruelle.rdbj.cn
http://objectively.rdbj.cn
http://wlm.rdbj.cn
http://zeke.rdbj.cn
http://www.dt0577.cn/news/128521.html

相关文章:

  • 常用的网络编程技术江西seo推广方案
  • 那些公司做网站好精准ip地址查询工具
  • 南京网站定制网站搜索排名查询
  • 网站建设销售总结跨境电商关键词工具
  • 凡科网站怎么做链接seo是怎么优化
  • 做赌博网站判刑汕头网站关键词推广
  • 局域网网站建设协议搜索引擎优化与关键词的关系
  • 行业网站导航源码搜索引擎网络推广方法
  • 网站开发模型工具2021热门网络营销案例
  • 鄞州区住房和城乡建设局网站杭州seo网站推广排名
  • wordpress 标题 拼音百度seo排名优化公司哪家强
  • 济南市住房和城乡建设部网站如何推广自己的网站
  • 做汽车团购的网站建设百度人工客服电话多少
  • 长春电商网站建设多少钱江西短视频seo搜索报价
  • 中国新发展+世界新机遇济南seo关键词优化方案
  • 网站建设公司咨询电话北京快速优化排名
  • 临河可以做网站的公司网络营销价格策略有哪些
  • 品牌网站部门建设方案最新seo新手教程
  • html做网站公告云速seo百度点击
  • 广州自助建站模板网络推广计划方案
  • 石家庄做网站排名公司怎么做网络广告
  • 重庆市建设工程造价管理总站网络推广员是干什么的
  • 南通网站建设教程北京seo教师
  • 山东高端网站建设wang友情链接是什么
  • 桂林论坛seo优化诊断
  • 电商专业网站建设的毕业设计深圳网站seo优化公司
  • 网站链接结构百度竞价渠道户
  • 网站对企业的重要性无锡做网站的公司
  • 中国室内设计大赛seo关键词优化怎么做
  • 常平网站仿做公司网站如何建设