環(huán)境
- Flutter 2.3.0
- Dart 2.14.0
準(zhǔn)備
- 配置好Flutter環(huán)境兴革,具體細(xì)節(jié) 參考Flutter官網(wǎng)
- 終端命令行創(chuàng)建Flutter Module
flutter create --template module flutter_module
- Xcode 創(chuàng)建 iOS Project(FlutterDemo)
- 創(chuàng)建并配置 Podfile 文件
$flutter_path = '../flutter_module/'
target 'FlutterDemo' do
flutter_application_path = $flutter_path
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
install_all_flutter_pods(flutter_application_path)
end
- pod install
頁面跳轉(zhuǎn)
AppDelegate.swift
import Flutter
lazy var flutterEngine: FlutterEngine = {
// "main" 為對(duì)應(yīng)的dart文件
let engine: FlutterEngine = FlutterEngine(name: "main", project: nil)
return engine
}()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
flutterEngine.run(withEntrypoint: nil)
return true
}
Native跳轉(zhuǎn)Flutter頁面
let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
let vc: FlutterViewController = FlutterViewController(engine: appDelegate.flutterEngine, nibName: nil, bundle: nil)
navigationController?.pushViewController(vc, animated: true)
使用CocoaPods引入遠(yuǎn)程倉庫Flutter Module
- 上面集成是通過指定本地路徑Flutter文件锯梁,才能正確引入
- 創(chuàng)建Flutter的時(shí)候?qū)⒛夸浀刂穾нM(jìn)了文件內(nèi) 導(dǎo)致如果將Flutter Module地址移動(dòng)地址浙巫,將不能正確runing 主Project
實(shí)際開發(fā)過程中难菌,往往將Module直接放在遠(yuǎn)程倉庫上侵续,需要的時(shí)候再通過pod install集成到主項(xiàng)目中燎潮, 基于以上兩點(diǎn)喻鳄,上面實(shí)現(xiàn) 無法滿足實(shí)際需求。
解決方案:Flutter Module 生成 Framework
- 先打包Framework
flutter build ios-framework --output=./path
- 將導(dǎo)出的所有Framework都合并架構(gòu)
/// 查看 framework 支持的架構(gòu)
lipo -info Flutter.framework
/// 合并架構(gòu)
lipo -create Flutter-1.framework/Flutter Flutter-2.framework/Flutter -output Flutter.framework
- 創(chuàng)建私有庫
pod lib create FlutterLib
4.修改podspec文件, 添加依賴flutter module打包生成所有framework
s.ios.vendored_frameworks = 'ios_frameworks/App.framework',
'ios_frameworks/Flutter.framework'
- 將第2步合并的所有framework移動(dòng)到 第4步指定的目錄中确封,如ios_frameworks目錄下
- 在主app下通過引用私有庫 FlutterLib 引入Flutter Module