flutter集成到現(xiàn)有的iOS項(xiàng)目中

1.在iOS項(xiàng)目所在的同級(jí)目錄下創(chuàng)建flutter_module獨(dú)立的項(xiàng)目

?在終端上輸入命令$ flutter create -t module flutter_module



2.在IOS項(xiàng)目的Podfile中添加以下代碼,給工程target配置flutter相關(guān)的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目錄上執(zhí)行'flutter build ios'

$ flutter build ios (flutter項(xiàng)目有修改的東西更新之后 如果要同步到iOS項(xiàng)目 需要重新 執(zhí)行 flutter build ios)

4.在IOS工程目錄上執(zhí)行'pod install',完成之后就可以在IOS工程中看到flutter及依賴庫的Framework和源碼

pod install

5.修改bitcode

6.在項(xiàng)目中的一個(gè)控制器中導(dǎo)入Flutter 創(chuàng)建一個(gè)按鈕點(diǎn)擊進(jìn)入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:@"進(jìn)入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 - 調(diào)用flutter的頁面
- (void)goFlutter{
??? [self.navigationController pushViewController:self.flutterVc animated:YES];
}

-(FlutterEngine *)flutterEngine{
??? if (!_flutterEngine) {//main對(duì)應(yīng)main.dart

??????? FlutterEngine * engine = [[FlutterEngine alloc] initWithName:@"main"];
??????? if (engine.run) {
??????????? _flutterEngine = engine;
??????? }
??? }
??? return _flutterEngine;
}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容