美文网首页ios 自己
Flutter - 混编开发 IOS接入

Flutter - 混编开发 IOS接入

作者: Th丶小伟 | 来源:发表于2021-05-07 17:42 被阅读0次

    由于Flutter 的渲染机制。直接使用FlutterViewController去显示界面 会造成2M左右的内存无法释放问题。每次显示 关闭都会出现这个问题。内存泄漏。
    使用单例对FlutterViewController进行全局化 会解决这个问题。一旦生成则不会被释放

    原生与Flutter 代码的Channel通道代码需要一直
    
    使用
     [[JWFlutterManager shareInstance] pushViewControllerTag:@"页面名字" viewController:self];
     
    .h
    #import <Flutter/Flutter.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface JWFlutterManager : NSObject
    +(instancetype)shareInstance;
    //  所需要显示的页面名
    -(void)pushViewControllerTag:(NSString*)tag pushViewControllerTag:(UIViewController*)controller;
    @end
     
    
    
    .m
    #import "JWFlutterManager.h"
    #import <Flutter/Flutter.h>
    #import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h>
    #import "JWFlutterViewController.h"
    @interface JWFlutterManager()
    @property(nonatomic, strong) FlutterEngine* flutterEngine;
    @property(nonatomic, strong) JWFlutterViewController * flutterVc;
    @property(nonatomic, strong) FlutterMethodChannel * myMethodChannel;
    @property(nonatomic, strong) FlutterBasicMessageChannel* messageChannel;
    @end
    @implementation JWFlutterManager
    +(instancetype)shareInstance{
        static JWFlutterManager *flutterManager = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            flutterManager = [JWFlutterManager new];
            [flutterManager flutterConfiger];
        });
        return flutterManager;
    }
    -(void)flutterConfiger{
        self.flutterVc = [[JWFlutterViewController alloc] initWithEngine:self.flutterEngine nibName:nil bundle:nil];
        self.flutterVc.title = @"我的任务";
        self.flutterVc.modalPresentationStyle = 0;
        
        self.messageChannel = [FlutterBasicMessageChannel messageChannelWithName:@"接收点击消息Tag" binaryMessenger:self.flutterVc];
        // 接收消息监听
        __weak typeof(self) weakSelf = self;
        [self.messageChannel setMessageHandler:^(id message, FlutterReply callback) {
            NSString *messageId = message;
            NSLog(@"收到flutter传递的消息:%@",messageId);
            
        }];
          
    }
       
    -(void)pushViewControllerTag:(NSString*)tag pushViewControllerTag:(UIViewController*)controller{
        [self.messageChannel sendMessage:[self baseParams]];
        
        FlutterMethodChannel * methodChannel = [FlutterMethodChannel methodChannelWithName:@"页面间的消息Tag" binaryMessenger:self.flutterVc];
        [methodChannel invokeMethod:tag arguments:nil];
        _myMethodChannel = methodChannel;
        [self.flutterVc flutterViewController:tag];
        self.flutterVc.myMethodChannel = _myMethodChannel;
         
      [controller.navigationController pushViewController:self.flutterVc animated:YES];
      
    }
     
     
    // 网络配置
    -(NSDictionary*)baseParams{
        
        // 获取参数
        NSMutableDictionary *params = @{};
         
        NSDictionary *dic = @{@"baseURL":@"",
                              @"baseParam":params
        }; 
        return dic; 
    }
    -(FlutterEngine *)flutterEngine
    {
        if (_flutterEngine == nil) {
            FlutterEngine * flutterEngine = [[FlutterEngine alloc] initWithName:@"flutter_engine"];
            [flutterEngine runWithEntrypoint:nil];
            [GeneratedPluginRegistrant registerWithRegistry:flutterEngine];
            _flutterEngine = flutterEngine;
        }
        return _flutterEngine;
    }
    @end
    
    
    // JWFlutterManager.h 继承 FlutterViewController 可控性大
    .h
    #import <Flutter/Flutter.h>
    #import "JWFlutterManager.h"
    NS_ASSUME_NONNULL_BEGIN
    
    @interface JWFlutterViewController : FlutterViewController
    @property(nonatomic, strong) FlutterMethodChannel * myMethodChannel;
    -(void)flutterViewController:(NSString*)pageName; 
    @end
    
    
    .m
    
    #import "JWFlutterViewController.h"
    @interface JWFlutterViewController ()
    @property (nonatomic,copy) NSString *pageName;
    
    @end
    
    @implementation JWFlutterViewController
      
    -(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        if(self.pageName){
          // 页面刷新
            [self.myMethodChannel invokeMethod:self.pageName arguments:nil];
        }
         
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
         
    }
    -(void)flutterViewController:(NSString*)pageName{
        self.pageName = pageName;
    }
      
    -(void)dealloc{
        NSLog(@"------JWFlutterViewController-----dealloc------");
    }
    @end
    

    相关文章

      网友评论

        本文标题:Flutter - 混编开发 IOS接入

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