美文网首页Flutter for iOS
flutter pod接入和交互

flutter pod接入和交互

作者: 鸿伟x | 来源:发表于2019-02-28 12:19 被阅读0次

1如何集成

01修改Podfile文件引入Flutter

pod 'DATKFlutterSDK', :git => 'git@git.duia.org.cn:ios/DATKFlutterSDK.git', :tag => '1.0.5'

2如何跳转

    //在控制器中, 先导入 GeneratedPluginRegistrant
    //#import <DATKFlutterSDK/GeneratedPluginRegistrant.h>
    //然后就可以直接嗨拉~
    FlutterViewController* flutterViewController = [[FlutterViewController alloc] init];
    [GeneratedPluginRegistrant registerWithRegistry:flutterViewController];
    [self.navigationController pushViewController:flutterViewController animated:YES];

03如何交互

01.flutter主动调用iOS原生的数据
    //创建FlutterViewController
    FlutterViewController* flutterViewController = [[FlutterViewController alloc] init];
    //注册插件 (不写这句, flutter里的第三方插件有可能会失效)
    [GeneratedPluginRegistrant registerWithRegistry:flutterViewController];
    //创建MethodChannel
    FlutterMethodChannel* batteryChannel = [FlutterMethodChannel
                                            methodChannelWithName:@"samples.flutter.io/responseFlutter"
                                            binaryMessenger:flutterViewController];
    //注册flutter调用原生的监听,
    [batteryChannel setMethodCallHandler:^(FlutterMethodCall* call,
                                           FlutterResult result) {
        //flutter代码里调用 iOS平台方法时 会进此block
        //根据不同的方法名, 返回不同的信息
        if ([@"getUserData" isEqualToString:call.method]) {
            result(@"{\"name\":\"张三\",\"age\":\"10\"}");
        }if ([@"otherMethod" isEqualToString:call.method]) {
            result(@"返回其他的信息");
        } else {
            //调用的方法名不存在则返回FlutterMethodNotImplemented
            result(FlutterMethodNotImplemented);
        }
    }];
    
    [self.navigationController pushViewController:flutterViewController animated:YES];
02.iOS平台主动调用flutter代码

顺序是,先跳转flutter,等flutter初始化完成, 通过1个回调方法拿到FlutterEventSink,
然后就可以通过FlutterEventSink尽情的调用flutter里的代码了

01跳转flutterViewController之前先注册StreamHandler

  //先注册Stream监听
  FlutterEventChannel* chargingChannel = [FlutterEventChannel
      eventChannelWithName:@"samples.flutter.io/requestFlutter"
           binaryMessenger: flutterViewController];
  [chargingChannel setStreamHandler:self];//需实现FlutterStreamHandler代理

  ///注册完成后可以跳转flutter控制器了
  // [self.navigationController pushViewController:flutterViewController animated:YES];

02通过onListenWithArguments方法拿到FlutterEventSink

#pragma mark - <FlutterStreamHandler>
// // 这个onListen是Flutter端开始监听这个channel时的回调,第二个参数 EventSink是用来传数据的载体。
- (FlutterError* _Nullable)onListenWithArguments:(id _Nullable)arguments
                                       eventSink:(FlutterEventSink)eventSink {
    _eventSink = eventSink;
    if (_eventSink){//拿到_eventSink, 可以在任意地方调用eventSink
        eventSink(@"哈哈,Fluter你好啊~");
        eventSink(@"噶为何噶文化委");
    }
    return nil;
}

/// flutter不再接收
- (FlutterError* _Nullable)onCancelWithArguments:(id _Nullable)arguments {
    // arguments flutter给native的参数
    _eventSink = nil;
    return nil;
}

(由于iOS和flutter的交互都是异步进行的,我们可以把flutter理解成'远程服务器', 方法调用理解成'网络接口调用')


ViewController.m 完整代码

#import "ViewController.h"
#import <DATKFlutterSDK/GeneratedPluginRegistrant.h>

@interface ViewController ()<FlutterStreamHandler>
@end
@implementation ViewController{
    FlutterEventSink _eventSink;
}
- (void)viewDidLoad {
    [super viewDidLoad];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //创建FlutterViewController
    FlutterViewController* flutterViewController = [[FlutterViewController alloc] init];
    //注册插件 (不写这句, flutter里的第三方插件有可能会失效)
    [GeneratedPluginRegistrant registerWithRegistry:flutterViewController];
    //创建MethodChannel
    FlutterMethodChannel* batteryChannel = [FlutterMethodChannel
                                            methodChannelWithName:@"samples.flutter.io/daMethod"
                                            binaryMessenger:flutterViewController];
    //注册flutter调用原生的监听,
    [batteryChannel setMethodCallHandler:^(FlutterMethodCall* call,
                                           FlutterResult result) {
        //flutter代码里调用 iOS平台方法时 会进此block
        //根据不同的方法名, 返回不同的信息
        if ([@"getUserData" isEqualToString:call.method]) {
            result(@"{\"name\":\"张三\",\"age\":\"10\"}");
        }if ([@"otherMethod" isEqualToString:call.method]) {
            result(@"返回其他的信息");
        } else {
            //调用的方法名不存在则返回FlutterMethodNotImplemented
            result(FlutterMethodNotImplemented);
        }
    }];
    FlutterEventChannel* chargingChannel = [FlutterEventChannel
                                            eventChannelWithName:@"samples.flutter.io/requestFlutter"
                                            binaryMessenger:flutterViewController];
    [chargingChannel setStreamHandler:self];//需实现FlutterStreamHandler代理
    [self.navigationController pushViewController:flutterViewController animated:YES];
}

#pragma mark - <FlutterStreamHandler>
// // 这个onListen是Flutter端开始监听这个channel时的回调,第二个参数 EventSink是用来传数据的载体。
- (FlutterError* _Nullable)onListenWithArguments:(id _Nullable)arguments
                                       eventSink:(FlutterEventSink)eventSink {
    _eventSink = eventSink;
    if (_eventSink){//拿到_eventSink, 可以在任意地方调用eventSink
        eventSink(@"哈哈,Fluter你好啊~");
        eventSink(@"噶为何噶文化委");
    }
    return nil;
}
/// flutter不再接收
- (FlutterError* _Nullable)onCancelWithArguments:(id _Nullable)arguments {
    // arguments flutter给native的参数
    _eventSink = nil;
    return nil;
}
@end

相关文章

网友评论

    本文标题:flutter pod接入和交互

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