iOS老项目中添加flutter页面
2022-11-13 本文已影响0人
小明2021
官方文档地址:
https://flutter.cn/docs/development/add-to-app/ios/project-setup
Tip: 修复真机单独启动iOS项目打开flutter页面崩溃的bug
进入文件:flutter/packages/flutter_tools/bin/xcode_backend.dart
修改配置:(但是z)
String parseFlutterBuildMode() {
// Use FLUTTER_BUILD_MODE if it's set, otherwise use the Xcode build configuration name
// This means that if someone wants to use an Xcode build config other than Debug/Profile/Release,
// they _must_ set FLUTTER_BUILD_MODE so we know what type of artifact to build.
final String? buildMode = (environment['FLUTTER_BUILD_MODE'] ?? environment['CONFIGURATION'])?.toLowerCase();
if (buildMode != null) {
if (buildMode.contains('release')) {
return 'release';
}
if (buildMode.contains('profile')) {
return 'profile';
}
if (buildMode.contains('debug')) {
// return 'debug';
return 'release'; // 修复iOS项目添加flutter后,真机启动后崩溃的问题
}
}
自己总结步骤
1、创建 Flutter module
// 进入iOS项目的同级目录,执行:
flutter create --template module my_flutter
2、修改flutter配置
1、先于 my_flutter 目录运行 flutter pub get 以生成 .ios/ 目录。
2、把iOS项目的Runner的build id 改名例如:com.example.flutterm1121 (目的是保证编译成功)
3、进入:my_flutter/pubspec.yaml
修改例如: iosBundleIdentifier: com.example.flutterm1121 (跟Runner项目的build id 保持一致)
4、在flutter项目目录下执行:flutter build ios
3、修改iOS老项目配置
1、podfile文件添加如下代码(注意空格和换行),并执行pod install
# 导入flutter页面需要的
flutter_application_path = '../flutter_m'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
target 'XMUI_OC' do
use_frameworks!
# 导入flutter页面需要的
install_all_flutter_pods(flutter_application_path)
end
# 新增的配置 - # 导入flutter页面需要的
post_install do |installer|
flutter_post_install(installer) if defined?(flutter_post_install)
end
4、注意事项
当你在 pubspec.yaml 改变了 Flutter plugin 依赖,需要在 Flutter module 目录运行 flutter pub get,然后再次在你的iOS 项目中 运行 pod install.
5、iOS项目弹出flutter页面的代码添加
//
// TestFlutterVC.m
// XMUI_OC
//
// Created by zhangmingwei on 2022/11/10.
//
#import "TestFlutterVC.h"
#import "AppDelegate.h"
@import UIKit;
@import Flutter;
@interface TestFlutterVC ()
@property (nonatomic,strong) FlutterEngine *flutterEngine;
@end
@implementation TestFlutterVC
- (void)viewDidLoad {
[super viewDidLoad];
[self.customNaviView setTitleStr:@"测试flutter项目"];
self.view.backgroundColor = [UIColor lightGrayColor];
// Make a button to call the showFlutter function when pressed.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(showFlutter)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show Flutter!" forState:UIControlStateNormal];
button.backgroundColor = UIColor.blueColor;
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self action:@selector(showFlutter2)
forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:@"Show Flutter 2" forState:UIControlStateNormal];
button2.backgroundColor = UIColor.blueColor;
button2.frame = CGRectMake(80.0, 300.0, 160.0, 40.0);
[self.view addSubview:button2];
// ios项目添加flutter页面
self.flutterEngine = [[FlutterEngine alloc] initWithName:@"engine1"];
// Runs the default Dart entrypoint with a default Flutter route.
[self.flutterEngine run];
// Used to connect plugins (only if you have plugins with iOS platform code).
[GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine];
}
- (void)showFlutter {
FlutterEngine *flutterEngine =
((AppDelegate *)UIApplication.sharedApplication.delegate).flutterEngine;
FlutterViewController *flutterViewController =
[[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
[self presentViewController:flutterViewController animated:YES completion:nil];
}
- (void)showFlutter2 {
FlutterEngine *flutterEngine = self.flutterEngine;
FlutterViewController *flutterViewController =
[[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
[self.navigationController pushViewController:flutterViewController animated:YES];
}
@end