创建Flutter模块
在现有的iOS工程平级目录下,创建Flutter模块:
flutter create -t module flutter_module
在 my_flutter 目录中 flutter 会创建一些 Dart 代码,一些 Ruby脚本,启动项以及一个隐藏的.ios子目录。
使用 cocoapods + git 来管理
接着在你的 Podfile 文件中添加 Flutter 提供好的脚本:
source 'https://github.com/CocoaPods/Specs.git'use_frameworks!platform :ios, '10.0'inhibit_all_warnings!flutter_application_path = '../flutter_module'load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')target :ZhouApp do# 安装Flutter模块install_all_flutter_pods(flutter_application_path)end
其实这一行脚本会从.ios目录中安装 Flutter 的引擎。
如果更改Flutter插件依赖性时 flutter_module/pubspec.yaml,请flutter pub get 在Flutter模块目录中运行以刷新podhelper.rb脚本读取的插件列表。然后,pod install从再次从您的应用程序运行some/path/MyApp。
$ pod install
等待安装完成。
打开你的 AppDelegate,修改如下:
import Flutter@mainclass AppDelegate: UIResponder, UIApplicationDelegate {///初始化Flutter引擎lazy var engine:FlutterEngine = FlutterEngine.init(name: "zhou");func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {// Override point for customization after application launch.self.engine.run()return true}}
接着,我们来定义一个 ViewController,将 FlutterViewController 添加进去:
let engine = (UIApplication.shared.delegate as! AppDelegate).enginelet controller = FlutterViewController.init(engine: engine, nibName: nil, bundle: nil);controller.modalPresentationStyle = .fullScreenself.present(controller, animated: true, completion: nil)
Flutter模块调试
首先在命令行启动flutter的监听
flutter attach
如果有多台设备,需要选择一下设备
flutter attach -d 设备标志
--app-id后面跟的是 应用的bundId-d是应用的设备id
可以直接执行 flutter attach 直接调试,但是如果有个多个应用或者设备需要指定对应的设备

