方案一
1.創建flutter module
flutter create --template module my_flutter
2.配置Podfile文件,引入flutter module
flutter_application_path = '../my_flutter'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
3.執行pod install
4.引用Flutter頭文件
#import <Flutter/Flutter.h>
#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h>
5.示例
6.效果
7.注意
1)報錯找不到podhelper.rb
文件
需要通過vscode或者其他編譯器運行flutter項目,會生產podhelper.rb
文件
方案二
將flutter以framework的形式通過Cocoapods引入iOS工程
1.創建外層文件夾,包含ios工程 & pod工程
2.在pod工程中導入flutter工程
3.在flutter工程中即flutter_module_for_ios,創建move_file.sh,此腳本為了自動生成build產物,然后將所有的framework匯總到ios_frameworks中
4.配置spec文件
s.static_framework = true
p = Dir::open("ios_frameworks")
arr = Array.new
arr.push('ios_frameworks/*.framework')
s.ios.vendored_frameworks = arr
5.iOS工程可以通過引入本地Pod的方式,進行使用
配置Podfile文件
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'FlutterProject' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for FlutterProject
pod 'MyFlutterPod', :path => '../MyFlutterPod'
target 'FlutterProjectTests' do
inherit! :search_paths
# Pods for testing
end
target 'FlutterProjectUITests' do
# Pods for testing
end
end
6.執行pod install
知識點總結
1.flutter工程使用插件(三方庫)
需要配置pubspec.yaml
文件
例如:
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
#添加 數據持久化插件 https://pub.flutter-io.cn/packages/shared_preferences
shared_preferences: ^0.5.4+3
配好后,執行命令安裝flutter pub get
2.編譯flutter生成debug環境編譯產物
flutter build ios --debug
// 編譯debug產物
或者
flutter build ios --release --no-codesign
// 編譯release產物
3.創建pod模版
pod lib create xxx