MacOS环境下真机部署Flutter
在MacOS环境下部署Flutter到iPhone真机,稍微费了点周折,下面的步骤是经过实际测试通过的说明。有一个非常重要的前提就是一定记得把Xcode、MacOS和iPhone的iOS系统升级到当前到最新版本,减少不必要的设置。
我通过测试的系统的版本:
1. Xcode 13.3
2. MacOS 12.3
3. iPhone iOS15.4
一、搭建MacOS下的Flutter环境
到官网下载Flutter SDK https://docs.flutter.dev/get-started/install/macos。下载完毕后,默认是放到Downloads文件夹下,然后就需要添加到PATH。
在终端输入如下命令:
export PATH=~/Downloads/flutter/bin:$PATH
OK之后就可以执行flutter doctor / flutter upgrade等命令了。
二、VSCode运行Flutter项目
此时用VSCode打开Flutter项目会有相应提示,主要有以下两步操作:
1. 设置Flutter SDK的路径为Downloads/flutter(这是我的下载的Flutter SDK的路径)。
2. 安装Dart扩展
安装成功之后就可以直接运行Flutter项目(VSCode-运行-以非调试模式运行),选择Dart&Flutter。
三、错误解决
1. cocoapods not installed
sudo gem install cocoapods
2. Could not build the precompiled application for the device.
══════════════════════════════════════════════════════════
Building a deployable iOS app requires a selected Development Team with a
Provisioning Profile. Please ensure that a Development Team is selected by:
1- Open the Flutter project's Xcode target with
open ios/Runner.xcworkspace
2- Select the 'Runner' project in the navigator then the 'Runner' target
in the project settings
3- Make sure a 'Development Team' is selected under Signing & Capabilities > Team.
You may need to:
- Log in with your Apple ID in Xcode first
- Ensure you have a valid unique Bundle ID
- Register your device with your Apple Developer Account
- Let Xcode automatically provision a profile for your app
4- Build or run your project again
For more information, please visit:
https://flutter.dev/docs/get-started/install/macos#deploy-to-ios-devices
Or run on an iOS simulator without code signing
══════════════════════════════════════════════════════════
然后接上iphone真机,直接在VSCode选择真机(确保在线,可执行flutter doctor测试),F5启动Flutter项目安装。
此时观察iphone,发现flutter已经安装上了,但打不开,需要添加信任(设置-通用-VPN与设备管理-添加信任)。再打开flutter app,发现提示下列错误:
Alternatively,build in profile or release modes to enable launching from the home screen.
当然,如果想在Xcode里以release模式运行flutter项目,可以选择Xcode-Product-Scheme-Edit Scheme,看到如下窗口:
将debug切换为release然后用Xcode运行即可。
参考资料:
https://docs.flutter.dev/get-started/install/macos#deploy-to-ios-devices