1.在iOS项目所在的同级目录下创建flutter_module独立的项目
在终端上输入命令$ flutter create -t module flutter_module
2.在IOS项目的Podfile中添加以下代码,给工程target配置flutter相关的pods
target 'iOSFlutterDemo' do
flutter_application_path = '../flutter_module'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
install_all_flutter_pods(flutter_application_path)
end
3.在flutter_module目录上执行'flutter build ios'
$ flutter build ios (flutter项目有修改的东西更新之后 如果要同步到iOS项目 需要重新 执行 flutter build ios)
4.在IOS工程目录上执行'pod install',完成之后就可以在IOS工程中看到flutter及依赖库的Framework和源码
pod install
5.修改bitcode
6.在项目中的一个控制器中导入Flutter 创建一个按钮点击进入flutter页面
#import "HomeViewController.h"
@import Flutter;
@interface HomeViewController ()
@property (nonatomic, strong) FlutterEngine *flutterEngine;
@property(nonatomic, strong) FlutterViewController* flutterVc;
@property(nonatomic, strong) FlutterBasicMessageChannel * msgChannel;
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.whiteColor;
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(200, 100, 140, 45)];
btn.center = self.view.center;
btn.backgroundColor = UIColor.redColor;
[self.view addSubview:btn];
[btn setTitle:@"进入flutter页面" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(goFlutter) forControlEvents:UIControlEventTouchUpInside];
self.flutterVc = [[FlutterViewController alloc] initWithEngine:self.flutterEngine nibName:nil bundle:nil];
self.msgChannel = [FlutterBasicMessageChannel messageChannelWithName:@"messageChannel" binaryMessenger:self.flutterVc.binaryMessenger];
[self.msgChannel setMessageHandler:^(id _Nullable message, FlutterReply _Nonnull callback) {
NSLog(@"收到Flutter的:%@",message);
}];
}
#pragma mark - 调用flutter的页面
- (void)goFlutter{
[self.navigationController pushViewController:self.flutterVc animated:YES];
}
-(FlutterEngine *)flutterEngine{
if (!_flutterEngine) {//main对应main.dart
FlutterEngine * engine = [[FlutterEngine alloc] initWithName:@"main"];
if (engine.run) {
_flutterEngine = engine;
}
}
return _flutterEngine;
}
@end