当前位置:   article > 正文

flutter调用iOS源码_flutter ios registerwithregistrar:

flutter ios registerwithregistrar:

1.创建FlutterNativePlugin.h

#import <Flutter/Flutter.h>
#import <UIKit/UIKit.h>

@interface AppDelegate : FlutterAppDelegate

@end

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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);
    }
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/145264
推荐阅读
相关标签
  

闽ICP备14008679号