当前位置:   article > 正文

Flutter和iOS互相调用_flutter ios 交互

flutter ios 交互

flutter调用ios并有返回值:

  1. BXTestChannel.shared.initEventChannel(context);
  2. String a = await BXTestChannel.shared.getIosString();
  3. dlog(a);

flutter中的channel类:

  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'dart:async';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:wiki_flu/providers.dart';
  7. import 'package:wiki_flu/wiki_flu.dart';
  8. const String channelName_wikifxsaveLaunage = 'com.wikifx.BXTestChannel';
  9. const String method_BXTestChannel = 'method_BXTestChannel';
  10. class BXTestChannel {
  11. static final BXTestChannel shared = BXTestChannel._();
  12. BXTestChannel._();
  13. factory BXTestChannel() => shared;
  14. static final _channel = const MethodChannel(channelName_wikifxsaveLaunage);
  15. ///flutter调ios
  16. Future<String> getIosString() async {
  17. var a = await _channel.invokeMethod(method_BXTestChannel, {
  18. "test": "bx",
  19. });
  20. return a;
  21. }
  22. ///ios调flutter
  23. 注册监听 安卓调用
  24. initEventChannel(BuildContext context) {
  25. _channel.setMethodCallHandler((call) async {
  26. if (call.method == "method_BXTestChannel_from_ios") {
  27. Map payResult = Map<String, dynamic>.from(call.arguments);
  28. dlog("收到ios消息:${payResult.toString()}");
  29. }
  30. return null;
  31. });
  32. }
  33. }

ios中appdelegate方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中添加:

 [[BXTestChannel shareBXTestChannel] initWithFlutterViewController:flutterViewController];

ios中BXTestChannel.h

  1. //
  2. // BXTestChannel.h
  3. // Runner
  4. //
  5. // Created by bianxiang on 2022/12/12.
  6. //
  7. #import <Foundation/Foundation.h>
  8. NS_ASSUME_NONNULL_BEGIN
  9. @interface BXTestChannel : NSObject
  10. + (instancetype)shareBXTestChannel;
  11. - (void)initWithFlutterViewController:(FlutterViewController *)flutterViewController;
  12. @end
  13. NS_ASSUME_NONNULL_END

ios中BXTestChannel.m

  1. //
  2. // BXTestChannel.m
  3. // Runner
  4. //
  5. // Created byon 2022/6/20.
  6. //
  7. #import "BXTestChannel.h"
  8. #import "KKUpgradeView.h"
  9. #import <GT3Captcha/GT3Captcha.h>
  10. #import <WebKit/WebKit.h>
  11. #import "WXApi.h"
  12. #import "Config.h"
  13. #import <ShareSDK/ShareSDk.h>
  14. @interface BXTestChannel()
  15. @property(nonatomic, strong)FlutterViewController * flutterViewController;
  16. @property(nonatomic, strong)FlutterMethodChannel * methodChannel;
  17. @property(nonatomic, copy)FlutterResult flutterResult;
  18. @end
  19. @implementation BXTestChannel
  20. + (instancetype)shareBXTestChannel {
  21. static BXTestChannel * _launageChannel = nil;
  22. static dispatch_once_t onceToken;
  23. dispatch_once(&onceToken, ^{
  24. //不能再使用alloc方法
  25. //因为已经重写了allocWithZone方法,所以这里要调用父类的分配空间的方法
  26. _launageChannel = [[super allocWithZone:NULL] init];
  27. });
  28. return _launageChannel;
  29. }
  30. // 防止外部调用alloc 或者 new
  31. + (instancetype)allocWithZone:(struct _NSZone *)zone {
  32. return [BXTestChannel shareBXTestChannel];
  33. }
  34. // 防止外部调用copy
  35. - (id)copyWithZone:(nullable NSZone *)zone {
  36. return [BXTestChannel shareBXTestChannel];
  37. }
  38. // 防止外部调用mutableCopy
  39. - (id)mutableCopyWithZone:(nullable NSZone *)zone {
  40. return [BXTestChannel shareBXTestChannel];
  41. }
  42. - (void)initWithFlutterViewController:(FlutterViewController *)flutterViewController{
  43. BXTestChannel.shareBXTestChannel.flutterViewController = flutterViewController;
  44. BXTestChannel.shareBXTestChannel.methodChannel = [FlutterMethodChannel
  45. methodChannelWithName:@"com.wikifx.BXTestChannel"
  46. binaryMessenger:flutterViewController.binaryMessenger];
  47. //接收flutter消息
  48. WEAK_SELF
  49. [BXTestChannel.shareBXTestChannel.methodChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult _Nonnull result) {
  50. NSLog(@"%@---%@",call.method,call.arguments);
  51. if([@"method_BXTestChannel" isEqualToString:call.method]) {
  52. NSString *param = call.arguments[@"test"];
  53. NSString * str = [NSString stringWithFormat:@"%s%@","iosString:",param];
  54. result(str);
  55. //延迟2秒主动发消息给flutter
  56. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  57. [self sendMsgToFlutter];
  58. });
  59. } else {
  60. result(FlutterMethodNotImplemented);
  61. }
  62. }];
  63. }
  64. //ios给flutter发消息
  65. - (void)sendMsgToFlutter{
  66. NSDictionary *dict = @{@"key1":@"Hello",@"key2":@"World"};
  67. [BXTestChannel.shareBXTestChannel.methodChannel invokeMethod:@"method_BXTestChannel_from_ios" arguments:dict];
  68. }
  69. @end

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/383252
推荐阅读
相关标签
  

闽ICP备14008679号