美文网首页Flutter开发圈
Flutter调用iOS原生方法

Flutter调用iOS原生方法

作者: 倪大头 | 来源:发表于2019-07-29 10:07 被阅读0次

    有点像是和 JS 的交互,Flutter 是通过自定义的 MethodChannel 来做通道

    Flutter 中的代码:

    //调用原生方法
    Future runiOSMethod() async {
      const platform = const MethodChannel('lianchu');
      var result;
      try {
        result = await platform.invokeMethod('comeonman');
        return Future.value(result);
      } on PlatformException catch (e) {
        return Future.error(e.toString());
      }
    }
    

    MethodChannel 对应通道,invokeMethod 对应方法,上面的代码调用了通道‘lianchu’的‘comeonman’方法

    iOS 中的代码:
    从这里可以直接在 XCode 中打开对应的 iOS 项目


    image.png

    先为 Flutter 方法创建一个工具类:

    FlutterNativePlugin.h:

    #import <Foundation/Foundation.h>
    #import <Flutter/Flutter.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface FlutterNativePlugin : NSObject <FlutterPlugin>
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    FlutterNativePlugin.m:

    #import "FlutterNativePlugin.h"
    
    @implementation FlutterNativePlugin
    
    + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
        FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"lianchu" binaryMessenger:[registrar messenger]];
        FlutterNativePlugin *instance = [[FlutterNativePlugin alloc]init];
        [registrar addMethodCallDelegate:instance channel:channel];
    }
    
    - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
        if ([call.method isEqualToString:@"comeonman"]) {
            result(@"么么哒");
        }else {
            result(FlutterMethodNotImplemented);
        }
    }
    
    @end
    

    上面代码找到了 MethodChannel('lianchu') 中的 Method('comeonman'),并返回了一个‘么么哒’

    接下来要在 AppDelegate.m 中完成注册

    AppDelegate.m:

    #include "AppDelegate.h"
    #include "GeneratedPluginRegistrant.h"
    #import "FlutterNativePlugin.h"
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      [GeneratedPluginRegistrant registerWithRegistry:self];
      
      [FlutterNativePlugin registerWithRegistrar:[self registrarForPlugin:@"FlutterNativePlugin"]];
        
      return [super application:application didFinishLaunchingWithOptions:launchOptions];
    }
    
    @end
    

    妥了,在 Flutter 代码中写个按钮调用一下试试效果:

    FlatButton(
      onPressed: () async {
        var futureValue = await runiOSMethod();
        return showDialog(
          context: context,
          builder: (context) {
            return AlertDialog(
              content: Text(
                 futureValue,
                 textAlign: TextAlign.center,
              ),
            );
          },                                
        );
      },
      child: Text(
        '我是按钮',
        style: TextStyle(fontSize: 12.0),
      ),
      color: Colors.red,
      textColor: Colors.white,
    )
    
    image.png

    相关文章

      网友评论

        本文标题:Flutter调用iOS原生方法

        本文链接:https://www.haomeiwen.com/subject/wasvrctx.html