Flutter调用原生方法并且传参
Flutter
Future reportError(errorJson)async{
const platform = const MethodChannel("com.xxx.xxx");
var result;
try{
result = await platform.invokeMethod('reportErrorMethod',errorJson);
return Future.value(result);
}on PlatformException catch(e){
return Future.error(e.toString());
}
}
OC
新建文件.h
#import <Foundation/Foundation.h>
#import <Flutter/Flutter.h>
NS_ASSUME_NONNULL_BEGIN
@interface FlutterNativePlugin : NSObject<FlutterPlugin>
@end
NS_ASSUME_NONNULL_END
新建文件.m
#import "FlutterNativePlugin.h"
@implementation FlutterNativePlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar{
FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"com.xxx.xxx" binaryMessenger:[registrar messenger]];
FlutterNativePlugin *instance = [[FlutterNativePlugin alloc]init];
[registrar addMethodCallDelegate:instance channel:channel];
}
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result{
if ([@"isChinese" isEqualToString:call.method]) {
result([NSNumber numberWithBool:YES]);
}else if([@"reportErrorMethod" isEqualToString:call.method]){
//flutter传给原生的参数
NSString * para = call.arguments;
if (para.length != 0) {
//OC传参给Flutter
result([NSString stringWithFormat:@"从原生返回过来的==>%@",para]);
}else{
//向flutter抛出异常
result([FlutterError errorWithCode:@"404" message:[NSString stringWithFormat:@"我是异常信息"] details:@"我是异常描述"]);
}
}else{
result (FlutterMethodNotImplemented);
}
}
@end
MethodChannel方式
待续
Plugin包
##需要引用iOS自定义framework库。
在podspec下, 通过s.subspec的方式,指定正确的framework路径
s.subspec 'xxxxFramework' do |ss|
ss.source_files = 'xxxxFramework/**/*.{h,m}'
ss.resource = 'xxxxFramework/**/*.{bundle,lic}'
ss.vendored_frameworks = 'xxxxFramework/xxxx.framework'
end
##需要引用iOS系统framework库。
在podspec下
#依赖系统Accelerate.framework
s.frameworks = 'Accelerate'
#依赖libc++.tbd , 去除lib前缀
s.libraries = 'c++'