Flutter和iOS互相调用

flutter调用ios并有返回值:

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

flutter中的channel类:

import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:wiki_flu/providers.dart';
import 'package:wiki_flu/wiki_flu.dart';


const String channelName_wikifxsaveLaunage = 'com.wikifx.BXTestChannel';

const String method_BXTestChannel = 'method_BXTestChannel';

class BXTestChannel {

  static final BXTestChannel shared = BXTestChannel._();
  BXTestChannel._();
  factory BXTestChannel() => shared;
  static final _channel = const MethodChannel(channelName_wikifxsaveLaunage);



  ///flutter调ios
  Future<String> getIosString() async {
    var a = await _channel.invokeMethod(method_BXTestChannel, {
      "test": "bx",
    });
    return a;
  }


  ///ios调flutter
注册监听 安卓调用
  initEventChannel(BuildContext context) {
    _channel.setMethodCallHandler((call) async {
      if (call.method == "method_BXTestChannel_from_ios") {
        Map payResult = Map<String, dynamic>.from(call.arguments);
        dlog("收到ios消息:${payResult.toString()}");
      }
      return null;
    });
  }

}

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

 [[BXTestChannel shareBXTestChannel] initWithFlutterViewController:flutterViewController];

ios中BXTestChannel.h

//
//  BXTestChannel.h
//  Runner
//
//  Created by bianxiang on 2022/12/12.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN


@interface BXTestChannel : NSObject

+ (instancetype)shareBXTestChannel;

- (void)initWithFlutterViewController:(FlutterViewController *)flutterViewController;
@end

NS_ASSUME_NONNULL_END


ios中BXTestChannel.m

//
//  BXTestChannel.m
//  Runner
//
//  Created by 苗 on 2022/6/20.
//

#import "BXTestChannel.h"
#import "KKUpgradeView.h"

#import <GT3Captcha/GT3Captcha.h>
#import <WebKit/WebKit.h>

#import "WXApi.h"
#import "Config.h"

#import <ShareSDK/ShareSDk.h>


@interface BXTestChannel()

@property(nonatomic, strong)FlutterViewController * flutterViewController;
@property(nonatomic, strong)FlutterMethodChannel * methodChannel;
@property(nonatomic, copy)FlutterResult flutterResult;

@end


@implementation BXTestChannel


+ (instancetype)shareBXTestChannel {
    static BXTestChannel * _launageChannel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //不能再使用alloc方法
        //因为已经重写了allocWithZone方法,所以这里要调用父类的分配空间的方法
        _launageChannel = [[super allocWithZone:NULL] init];
    });
    return _launageChannel;
}

// 防止外部调用alloc 或者 new
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [BXTestChannel shareBXTestChannel];
}

// 防止外部调用copy
- (id)copyWithZone:(nullable NSZone *)zone {
    return [BXTestChannel shareBXTestChannel];
}

// 防止外部调用mutableCopy
- (id)mutableCopyWithZone:(nullable NSZone *)zone {
    return [BXTestChannel shareBXTestChannel];
}

- (void)initWithFlutterViewController:(FlutterViewController *)flutterViewController{
    
    BXTestChannel.shareBXTestChannel.flutterViewController = flutterViewController;
    BXTestChannel.shareBXTestChannel.methodChannel = [FlutterMethodChannel
                                                    methodChannelWithName:@"com.wikifx.BXTestChannel"
                                                    binaryMessenger:flutterViewController.binaryMessenger];
    
    //接收flutter消息
    WEAK_SELF
    [BXTestChannel.shareBXTestChannel.methodChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
        NSLog(@"%@---%@",call.method,call.arguments);
        if([@"method_BXTestChannel" isEqualToString:call.method]) {
            NSString *param = call.arguments[@"test"];
            NSString * str = [NSString stringWithFormat:@"%s%@","iosString:",param];
            result(str);
           
        
            //延迟2秒主动发消息给flutter
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self sendMsgToFlutter];
             });
            
            
            
        } else {
            result(FlutterMethodNotImplemented);
        }
    }];
    
}

//ios给flutter发消息
- (void)sendMsgToFlutter{
    
    NSDictionary *dict = @{@"key1":@"Hello",@"key2":@"World"};
    
    [BXTestChannel.shareBXTestChannel.methodChannel invokeMethod:@"method_BXTestChannel_from_ios" arguments:dict];
    
}


@end

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值