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