iOS项目集成Flutter

iOS与Flutter混编目前有两种模式

一、iOS原生作为基座嵌入Flutter module(目前大部分为已存在的原生项目集成Flutter)

1、首先使用 flutter doctor 检测是否有完成的flutter环境;pod --version 检测cocopods的版本(版本必须大于1.10.0)

2、新建一个iOSApp IMPDemo并在项目同级目录中新建 flutter_module 文件夹(多个单词小写下划线分割), podfile文件,新建完成后结构如下:

├── IMPDemo
├── IMPDemo.xcodeproj
├── IMPDemo.xcworkspace
├── IMPDemoTests
├── IMPDemoUITests
├── flutter_module
└── podfile

3、编辑podfile文件,编辑完成后执行 pod install

#相对目录(注意:如多个单词 小写下划线分割,不能使用驼峰)
flutter_application_path = 'flutter_module'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

platform :ios, '11.0'
inhibit_all_warnings!

target 'IMPDemo' do
  install_all_flutter_pods(flutter_application_path)

  #其他的第三方库 如Masonry
  pod 'Masonry',                '1.1.0'
end

4、如果出现报错,则根据提示 执行对应的命令修复即可

5、在iOS项目中测试(单纯测试是否整合成功)

       1)在Appdelegate中注册一个Flutter引擎

#import "AppDelegate.h"
#import "ViewController.h"
#import 
@interface AppDelegate ()
@property(nonatomic, strong)FlutterEngine *engine;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    ViewController *rootVC = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootVC];
    
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];

    [self.engine run];
    return YES;
}

- (FlutterEngine *)engine {
    if (!_engine) {
        _engine = [[FlutterEngine alloc] initWithName:@"flutter_moudel"];
    }
    return _engine;
}

@end

  2)在页面在控制器中新建一个FlutterViewController 并跳转一个Flutter界面

#import "ViewController.h"
#import 
@interface ViewController ()

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.lightGrayColor;
}

//点击展示一个Flutter页面
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    FlutterViewController *vc = [[FlutterViewController alloc] initWithProject:nil nibName:nil bundle:nil];
    [self presentViewController:vc animated:true completion:nil];
}
@end

二、Flutter作为基座嵌入iOS

你可能感兴趣的:(flutter,ios,objective-c)