赞
踩
1.创建FlutterNativePlugin.h
#import <Flutter/Flutter.h>
#import <UIKit/UIKit.h>
@interface AppDelegate : FlutterAppDelegate
@end
2.创建FlutterNativePlugin.m
#import "FlutterNativePlugin.h" #import <Foundation/Foundation.h> @implementation FlutterNativePlugin + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar { FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"toios" binaryMessenger:[registrar messenger]]; FlutterNativePlugin *instance = [[FlutterNativePlugin alloc ] init]; [registrar addMethodCallDelegate:instance channel:channel]; [registrar addApplicationDelegate:instance]; } - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result { if ([call.method isEqualToString:@"test"]) { // 执行你的代码 //返回给flutter result(1); } } @end
3.覆盖AppDelegate.m
#import "AppDelegate.h"
#import "GeneratedPluginRegistrant.h"
#import "FlutterNativePlugin.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];
[FlutterNativePlugin registerWithRegistrar:[self registrarForPlugin:@"FlutterNativePlugin"]];
return YES;
}
@end
4 .flutter 发消息
// toios 标识 static const platform = const MethodChannel("toios"); getNative() async{ String result = ''; try { //参数为方法名称test。 getNative参数可以是任意类型 result = await platform.invokeMethod("test","getNative"); //结果返回result。 我ios成功返回的1,也可以返回调用ios运行的结果 if(result == 1){ //成功 //执行你的 }else{ //失败 //执行你的 } } on PlatformException catch (e) { print(e.message); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。