創(chuàng)建Flutter模塊
創(chuàng)建iOS工程native_flutter_demo
徙菠,在它的上一層目錄創(chuàng)建flutter模塊:
$ flutter create -t module flutter_module
如圖所示:
創(chuàng)建Podfile
進入到你的iOS工程目錄:
$ pod init
vim Podfile
編輯 Podfile叶沛,在文件末尾添加:
注意: Flutter不同版本蚯涮,Podfile是有差別的
查看flutter版本:
$ flutter --version
Flutter 1.10.15-pre.384 ? channel master ? https://github.com/flutter/flutter.git
Framework ? revision ee032f67c7 (5 days ago) ? 2019-11-02 09:46:25 -0700
Engine ? revision 8ea19b1c76
Tools ? Dart 2.6.0 (build 2.6.0-dev.8.2 bbe2ac28c9)
Flutter 1.8.4-pre.21之前:
target 'native_flutter_demo' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for native_flutter_demo
target 'native_flutter_demoTests' do
inherit! :search_paths
# Pods for testing
end
target 'native_flutter_demoUITests' do
inherit! :search_paths
# Pods for testing
end
end
flutter_application_path = '../flutter_module'
eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)
為編譯Dart 代碼添加build phase
打開iOS項目辈赋,選中TARGETnative_flutter_demo
項目的Build Phases
選項树肃,點擊左上角+
號按鈕,選擇New Run Script Phase
,將下面的shell腳本添加到輸入框中:
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed
如下圖所示:
Flutter 1.8.4-pre.21之后:
flutter_application_path = '../flutter_module'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
target 'native_flutter_demo' 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 FCPublicOpinionSystem
target 'native_flutter_demoTests' do
inherit! :search_paths
# Pods for testing
install_all_flutter_pods(flutter_application_path)
end
target 'native_flutter_demoUITests' do
inherit! :search_paths
# Pods for testing
install_all_flutter_pods(flutter_application_path)
end
end
其中flutter_application_path代表你的flutter模塊路徑。
運行
pod install
然后需要編譯一下項目:Command + B
惧所。
參考文章:
Add-Flutter-to-existing-apps
Upgrading-Flutter-added-to-existing-iOS-Xcode-project