iOS项目(Swift),使用Flutter进行混合开发

发布时间 2023-11-09 22:48:13作者: 玉米炖排骨

一、创建flutter_module

先创建flutter module项目,参考官方文档,先cd至目标文件夹,执行命令为

flutter create --template module flutter_module

创建成功后,打开flutter_module项目,打开pubspec.yaml文件,添加所需的依赖

 执行命令,更新依赖。

执行 flutter run 命令,生成pod

创建Swift项目,打开Swift项目中的 Podfile添加相关pod命令

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

flutter_application_path = './flutter_module模块项目'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

target 'FlutterMixTest' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!

# flutter
install_all_flutter_pods(flutter_application_path)
# Pods for FlutterMixTest
 
end

post_install do |installer|
flutter_post_install(installer) if defined?(flutter_post_install)
end

执行pod install命令(每次 flutter_module 中的 pubspec.yaml 更改后,都需要再次执行 pod install 命令)

 

我们在应用启动时的 App Delegate 中创建了一个 FlutterEngine 并作为属性暴露给外界。

在 SceneDelegate.swift 中

或 在 AppDelegate.swift:

 在view controller中使用

 

 

 

使用链接:https://flutter.cn/docs/development/add-to-app/ios/add-flutter-screen?tab=vc-uikit-swift-tab