image.png

创建Flutter模块

在现有的iOS工程平级目录下,创建Flutter模块:

  1. flutter create -t module flutter_module

在 my_flutter 目录中 flutter 会创建一些 Dart 代码,一些 Ruby脚本,启动项以及一个隐藏的.ios子目录。

使用 cocoapods + git 来管理

接着在你的 Podfile 文件中添加 Flutter 提供好的脚本:

  1. source 'https://github.com/CocoaPods/Specs.git'
  2. use_frameworks!
  3. platform :ios, '10.0'
  4. inhibit_all_warnings!
  5. flutter_application_path = '../flutter_module'
  6. load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
  7. target :ZhouApp do
  8. # 安装Flutter模块
  9. install_all_flutter_pods(flutter_application_path)
  10. end

其实这一行脚本会从.ios目录中安装 Flutter 的引擎。

如果更改Flutter插件依赖性时 flutter_module/pubspec.yaml,请flutter pub get 在Flutter模块目录中运行以刷新podhelper.rb脚本读取的插件列表。然后,pod install从再次从您的应用程序运行some/path/MyApp。

  1. $ pod install

等待安装完成。

打开你的 AppDelegate,修改如下:

  1. import Flutter
  2. @main
  3. class AppDelegate: UIResponder, UIApplicationDelegate {
  4. ///初始化Flutter引擎
  5. lazy var engine:FlutterEngine = FlutterEngine.init(name: "zhou");
  6. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  7. // Override point for customization after application launch.
  8. self.engine.run()
  9. return true
  10. }
  11. }

接着,我们来定义一个 ViewController,将 FlutterViewController 添加进去:

  1. let engine = (UIApplication.shared.delegate as! AppDelegate).engine
  2. let controller = FlutterViewController.init(engine: engine, nibName: nil, bundle: nil);
  3. controller.modalPresentationStyle = .fullScreen
  4. self.present(controller, animated: true, completion: nil)

Flutter模块调试

首先在命令行启动flutter的监听

  1. flutter attach

如果有多台设备,需要选择一下设备

  1. flutter attach -d 设备标志
  • --app-id后面跟的是 应用的bundId
  • -d 是应用的设备id

可以直接执行 flutter attach 直接调试,但是如果有个多个应用或者设备需要指定对应的设备
image.png

参考文档

  1. 将Flutter模块集成到您的iOS项目中