今天将flutter作为模块嵌入我们老的iOS项目中,编译没有报错,但是运行时一直出现 "unable to install "的错误,无法安装的手机。之前在Demo中是可以运行的,查阅一番资料后,找到了解决办法,记录一下。
前提是你已经配置好了flutter开发环境,没有配的小伙伴可以去flutter中文网,按照教程配置就可以了!
- 创建 flutter module:
cd 到项目文件的同级目录下;
执行命令:(my_flutter: 文件名称,可修改)
flutter create -t module my_flutter
- 通过cocoapods添加依赖
在项目的Podfile文件中添加如下代码:
#其中flutter_application_path为flutter模块相对于podfile文件的位置。 flutter_application_path = '/Users/admin/Desktop/flutter项目/my_flutter' load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb') target 'yiguan' do use_frameworks! # Flutter 本地开发调试 install_all_flutter_pods(flutter_application_path) target 'yiguanTests' do inherit! :search_paths end end
更新pods
pod install
- 添加脚本
在iOS项目中找到Xcode -> Target -> Build Phase 位置,点击➕好,选择New Run Script Phase;然后把以下两行命令放进去。
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build "$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed
- 编译项目
如果报如下错误:
/bin/sh: /packages/flutter_tools/bin/xcode_backend.sh: No such file or directory
需指定FLUTTER_ROOT路径,在 my_flutter/.ios(隐藏文件)/Flutter/Generated.xcconfig 路径下找到Generated.xcconfig 文件,打开可以看到如下图:
找到如下两行,添加到xcode -> target -> build setting - User-Defined 中:
// This is a generated file; do not edit or check into version control. FLUTTER_ROOT=/Users/admin/development/flutter FLUTTER_APPLICATION_PATH=/Users/admin/Desktop/flutter项目/my_flutter
如图: