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,發現提示下列錯誤:

In iOS 14+,debug mode Flutter apps can only be launched from Flutter tooling, IDEs with Flutter plugins or from Xcode.
Alternatively,build in profile or release modes to enable launching from the home screen.
 
提示很清楚,我的iPhone系統iOS版本是15.4,要運行debug模式,需要安裝Flutter插件或者直接從Xcode啟動。另外一種方式是直接以release模式運行。
這里我直接在Xcode里啟動,即可看到真機運行效果。

當然,如果想在Xcode里以release模式運行flutter項目,可以選擇Xcode-Product-Scheme-Edit Scheme,看到如下窗口:

將debug切換為release然后用Xcode運行即可。

 

 

參考資料:

https://docs.flutter.dev/get-started/install/macos#deploy-to-ios-devices

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM