Flutter混編-iOS集成
- 環境要求
- 創建Flutter module
- 集成modlue到iOS專案
- 方案A-通過CocoaPods和SDK集成
- 方案B1-通過frameworks在Xcode集成
- 方案B2-在B1的基礎上使用CocoaPods集成Flutter.framework
- 方案B3-最終方案
Flutter可以作為嵌入式框架添加到現有的iOS專案中,
環境要求
Flutter 支持iOS 8.0或以上,開發環境必須滿足以下條件:
- Xcode installed ;
- Operating Systems,macOS (64-bit) ;
- Disk Space,2.8 GB ;
- Tools,bash、rm、unzip、which … ;
創建Flutter module
用以下命令可以快速創建module, command line 運行:
cd some/path/
flutter create --template module my_flutter
集成modlue到iOS專案
有兩種方式去集成到專案,
- 通過CocoaPods依賴管理和安裝Flutter SDK;(官方推薦)
- 創建frameworks包含Dart代碼的Flutter engine和 Flutter 插件代碼,在Xcode 的 build settings 中更新frameworks即可,
方案A-通過CocoaPods和SDK集成
此方案要求在你的專案上作業的每個開發人員都必須在本地安裝了Flutter SDK, 只需在Xcode中構建應用程式即可自動運行腳本以嵌入Dart和插件代碼, 這允許使用Flutter模塊的最新版本進行快速迭代,而無需在Xcode之外運行其他命令,專案結構如下:


如果新建的專案沒有Podfile檔案,先根據文章 Adding Pods to an Xcode project 去添加一個Podfile,
- 在iOS專案(MyApp)中的Podfile檔案中加入
flutter_application_path = '../my_flutter'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
- 在Podfile target 需要集成flutter,install_all_flutter_pods(flutter_application_path)
target 'MyApp' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
install_all_flutter_pods(flutter_application_path)
# Pods for MyApp
end
- 最后運行 pod install 就可以通過CocoaPods集成flutter代碼到專案中
方案B1-通過frameworks在Xcode集成
通過flutter命令生產必要的frameworks,并通過手動嵌入到現在的Xcode專案中,這個方案不需要你的團隊開發成員本地安裝Flutter SDK和CocoaPods,但是在每次在module進行代碼更改時,都要運行 flutter build ios-framework 更新frameworks
- 執行生成framework命令
cd my_module
flutter build ios-framework --output=../MyApp/Flutter/


- 在Xcode中鏈接frameworks
Build Setting->Build Phases->Link Binary With Libraries->+>AddOther->Add Files,添加Release下的所有framework檔案

3. 最后在framework下拉串列中選擇Embed&Sign,這樣就完成frameworks集成

方案B2-在B1的基礎上使用CocoaPods集成Flutter.framework
這個方案就是避免把體積較大的Flutter.framework(大概100MB)的拷貝給其他開發人員,減輕持續集成的成本負擔,而是通過cocoapods加載遠程倉庫的包,
- 通過以下命令生成 Flutter.podspec
flutter build ios-framework --cocoapods --output=some/path/MyApp/Flutter/

#Flutter.podspec 檔案內容
Pod::Spec.new do |s|
s.name = 'Flutter'
s.version = '1.20.400' # 1.20.4
s.summary = 'Flutter Engine Framework'
s.description = <<-DESC desc DESC
s.homepage = 'https://flutter.dev'
s.license = { :type => 'MIT', :text => <<-LICENSE license LICENSE
}
s.author = { 'Flutter Dev Team' => 'flutter-dev@googlegroups.com' }
s.source = { :http => 'https://storage.flutter-io.cn/flutter_infra/flutter/d1bc06f032f9d6c148ea6b96b48261d6f545004f/ios/artifacts.zip' }
s.documentation_url = 'https://flutter.dev/docs'
s.platform = :ios, '8.0'
s.vendored_frameworks = 'Flutter.framework'
s.prepare_command = <<-CMD
unzip Flutter.framework -d Flutter.framework
CMD
end
- 添加Flutter到專案的Podfile
pod 'Flutter', :podspec => 'some/path/MyApp/Flutter/[build mode]/Flutter.podspec'
- App.framework, FlutterPluginRegistrant.framework按照方案B1的方式去集成
方案B3-最終方案
既然Flutter.framework可通過遠程倉庫的方式添加,App.framework, FlutterPluginRegistrant.framework也就可以通過發布到遠程倉庫,方便其他開發人員持續集成功能,
CocoaPods遠程管理簡單步驟如下,詳細步驟可參考文章 Cocoapods的使用
- 在github創建專案
- 初始化好專案(把framework檔案push上去)
- 到專案root目錄下, 創建 spec 檔案
cd 專案
pod spec create 名稱
- 編輯 spec 檔案
Pod::Spec.new do |s|
s.name = 'FlutterIosFramework'
#tag版本號
s.version = '0.0.7'
s.summary = 'High-performance, high-fidelity mobile apps.'
s.description = <<-DESC
Flutter provides an easy and productive way to build and deploy high-performance mobile apps for Android and iOS.
DESC
#自己填寫git地址
s.homepage = 'http://xxxx/FlutterIosFramework'
s.license = { :type => 'MIT' }
s.author = { 'Flutter Dev Team' => 'flutter-dev@googlegroups.com' }
#自己填寫git地址
s.source = { :git => 'http://gitlab.admin.bluemoon.com.cn/xxx/FlutterIosFramework', :tag => s.version.to_s }
s.ios.deployment_target = '9.0'
#s.xcconfig = { 'VALID_ARCHS' => 'arm64' }
s.pod_target_xcconfig = {'VALID_ARCHS' => 'armv7 arm64 x86_64' }
#這個是找到對應framework的路徑,請按照我的填寫來找到對應你自己的填寫
s.vendored_frameworks = '*.framework'
end
- 新建tag且提交專案
git add .
git commit -m '0.1.2'
git tag 0.1.2
git push --tags
git push origin master
- 驗證一下 podspec 檔案
pod lib lint --allow-warnings
- 如果是第一次提交需驗證一下,不是第一次可以不用此步
pod trunk register ios@bluemoon.com 'eric' --description=‘我是描述’
- 提交給 cocoapods 管理
pod trunk push FlutterIosFramework.podspec --allow-warnings --verbose
- 如果成功,可能需要等幾分鐘才可以 search 到你的 lib, 可去cocoapods查看
- 在IOS專案在Podfile檔案添加遠程倉庫鏈接,最后執行 pod install --repo-update 即可
target 'MyApp' do
use_frameworks!
pod 'FlutterIosFramework', '~> 0.1.2'
pod 'Flutter', :podspec => '../Flutter/Flutter.podspec'
# Pods for MyApp
end

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/221199.html
標籤:其他
下一篇:iOS 自定義可拖拽 panel
