进入正题之前,请先确认以下条件
1.Flutter环境已配置完成,并已经成功运行“hello world”;
2.一个OC语言对APP demo
开始
一、环境检查
$ flutter doctor
查看结果
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.7.8+hotfix.4, on Mac OS X 10.14.3 18D109, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 10.2.1)
[✓] iOS tools - develop for iOS devices
[✓] Android Studio (version 3.4)
[✓] Connected device (1 available)
• No issues found!
No issues found! 看似很完美,实际上这里有个坑,注意第一行的Channel,虽然官方推荐适用stable Channel,但是我们需要在Channel master下来进行iOS与flutter 对接。
切换到Channel master 的方法
# 查看可切换的分支
$ flutter channel
Flutter channels:
* stable
beta
dev
master
切换到master命令
$ flutter channel master
Switching to flutter channel 'master'...
git: Switched to a new branch 'master'
git: Branch 'master' set up to track remote branch 'master' from 'origin'.
再次执行doctor命令
Downloading Dart SDK from Flutter engine e7f9ef6aa0b9040102d1b3c9a6ae934df746ef94...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 267M 100 267M 0 0 260k 0 0:17:28 0:17:28 --:--:-- 427k
Building flutter tool...
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Downloading android-arm-profile/darwin-x64 tools... 20.7s
Downloading android-arm-release/darwin-x64 tools... 9.9s
Downloading android-arm64-profile/darwin-x64 tools... 12.2s
Downloading android-arm64-release/darwin-x64 tools... 10.6s
Downloading android-x86 tools...
150.8s (!)
Downloading android-x64 tools... 88.2s
Downloading android-arm tools... 40.4s
Downloading android-arm-profile tools... 20.2s
Downloading android-arm-release tools... 18.2s
Downloading android-arm64 tools... 29.5s
Downloading android-arm64-profile tools... 17.2s
Downloading android-arm64-release tools... 15.8s
Downloading package sky_engine... 6.0s
Downloading common tools... 34.6s
Downloading common tools... 40.7s
Downloading darwin-x64 tools...
159.5s (!)
Downloading libimobiledevice... 0.8s
Downloading usbmuxd... 0.1s
Downloading libplist... 0.1s
Downloading openssl... 2.7s
Downloading ideviceinstaller... 0.3s
Downloading ios-deploy... 0.1s
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, v1.9.8-pre.83, on Mac OS X 10.14.3 18D109, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 10.2.1)
[✓] Android Studio (version 3.4)
[✓] Connected device (1 available)
• No issues found!
确定当前Channel 在 master下,进入下一步。
二、Podfile 接入flutter module
文章开头提到所需要到Demo
2.1 如果你已经有创建好的flutter_module, 可以直接放到demo同一级的目录下,执行如下操作
$ cd
$ rm -rf .ios
$ flutter build ios
2.2 或者使用命令行创建一个新的flutter_module
$ cd
$ flutter create -t module
目前为止,所需要的条件就都已经准备好了,下面开始接入
2.3 在Podfile中添加路径
flutter_application_path = ''
# eg: flutter_application_path = '/Users/**/Desktop/ios_flutter/flutter_module'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
在target下添加引用
target 'Flutter_demo' do
install_all_flutter_pods(flutter_application_path)
end
然后执行
$ pod install
// 完成
Installing Flutter (1.0.0)
Installing FlutterPluginRegistrant (0.0.1)
Installing flutter_module (0.0.1)
Pod installation complete! There are 4 dependencies from the Podfile and 15 total pods installed.
注意:如果报错的话
[!] Invalid `Podfile` file: cannot load such file
解决方法:路径错误,请仔细检查 flutter_application_path
Invalid `Podfile` file: undefined method `pod' for main:Object
解决方法:先查看flutter的分支在master下,执行2.1操作;否则,回到文章开头,再执行一遍
2.4 关掉Bitcode
Flutter 目前不支持Bitcode,需要在设置中关掉.
Build Settings->Build Options->Enable Bitcode 设置为NO
三、开始撸代码
看下一篇iOS接入Flutter填坑记录-代码篇
参考文章:
1.Add Flutter to existing apps
2.Invalid Podfile
file: undefined method `pod' for main:Object. #37373