跳到主內容

將 Flutter 模組整合到您的 iOS 專案中

瞭解如何將 Flutter 模組整合到您現有的 iOS 專案中。

Flutter UI 元件可以逐步新增到您現有的 iOS 應用程式中,作為嵌入式框架。要將 Flutter 嵌入到您現有的應用程式中,請考慮以下三種方法之一。

嵌入方法方法論優勢
使用 CocoaPods (推薦) 安裝並使用 Flutter SDK 和 CocoaPods。Flutter 每次 Xcode 構建 iOS 應用時都會從原始碼編譯 flutter_module 將 Flutter 嵌入到您的應用中的最簡單方法。
使用 iOS 框架 為 Flutter 元件建立 iOS 框架,將其嵌入到您的 iOS 中,並更新您現有應用程式的構建設定。 不需要每個開發人員在其本地機器上安裝 Flutter SDK 和 CocoaPods。
使用 iOS 框架和 CocoaPods 將框架嵌入到您的 iOS 應用和外掛中,但將 Flutter 引擎作為 CocoaPods podspec 分發。 提供了一種替代方案,用於分發大型 Flutter 引擎 ( Flutter.xcframework ) 庫。

當您將 Flutter 新增到現有的 iOS 應用時,它會 增加 iOS 應用的大小

有關使用使用 UIKit 構建的應用的示例,請參閱 add_to_app 程式碼示例中的 iOS 目錄。有關使用 SwiftUI 的示例,請參閱 News Feed App 中的 iOS 目錄。

開發系統要求

#

Flutter 需要最新版本的 Xcode 和 CocoaPods

建立 Flutter 模組

#

要使用任何方法將 Flutter 嵌入到您現有的應用程式中,首先建立一個 Flutter 模組。使用以下命令建立 Flutter 模組。

cd /path/to/my_flutter
flutter create --template module my_flutter

Flutter 在 /path/to/my_flutter/ 下建立模組專案。如果您使用 CocoaPods 方法,請將模組儲存在與您現有的 iOS 應用相同的父目錄中。

從 Flutter 模組目錄,您可以執行與任何其他 Flutter 專案中相同的 flutter 命令,例如 flutter runflutter build ios。您還可以使用 Flutter 和 Dart 外掛在 VS CodeAndroid Studio/IntelliJ 中執行該模組。該專案包含模組的單檢視示例版本,在將其嵌入到您現有的 iOS 應用中之前。這有助於測試 Flutter 程式碼的獨立部分。

組織您的模組

#

my_flutter 模組目錄結構類似於典型的 Flutter 應用。

  • my_flutter/
    • .ios/
      • Runner.xcworkspace
      • Flutter/
        • podhelper.rb
    • lib/
      • main.dart
    • test/
    • pubspec.yaml

您的 Dart 程式碼應新增到 lib/ 目錄。您的 Flutter 依賴項、包和外掛必須新增到 pubspec.yaml 檔案中。

.ios/ 隱藏子資料夾包含一個 Xcode 工作區,您可以在其中執行模組的獨立版本。此包裝專案引導您的 Flutter 程式碼。它包含輔助指令碼,以方便構建框架或使用 CocoaPods 將模組嵌入到您現有的應用程式中。

將 Flutter 模組嵌入到您的 iOS 應用中

#

在您開發完 Flutter 模組後,可以使用頁面頂部的表格中描述的方法將其嵌入。

您可以在 Debug 模式下在模擬器或真機上執行,也可以在 Release 模式下在真機上執行。

使用 CocoaPods 和 Flutter SDK

#

方法

#

第一種方法使用 CocoaPods 嵌入 Flutter 模組。CocoaPods 管理 Swift 專案的依賴項,包括 Flutter 程式碼和外掛。每次 Xcode 構建應用時,CocoaPods 都會嵌入 Flutter 模組。

這允許您在無需在 Xcode 之外執行其他命令的情況下,使用最新版本的 Flutter 模組進行快速迭代。

要了解更多關於 CocoaPods 的資訊,請參閱 CocoaPods 入門指南

觀看影片

#

如果觀看影片有助於您學習,此影片涵蓋了將 Flutter 新增到 iOS 應用的過程

在 YouTube 上以新標籤頁觀看:“逐步演示如何在現有 iOS 應用中新增 Flutter”

要求

#

您的專案中的每個開發人員都必須在其本地機器上安裝 Flutter SDK 和 CocoaPods。

示例專案結構

#

本節假定您的現有應用和 Flutter 模組位於同級目錄中。如果您的目錄結構不同,請調整相對路徑。示例目錄結構如下所示

  • my_flutter/
    • .ios/
    • Flutter/
      • podhelper.rb
  • MyApp/
    • Podfile

更新您的 Podfile

#

將您的 Flutter 模組新增到您的 Podfile 配置檔案中。本節假定您將您的 Swift 應用命名為 MyApp

  1. (可選) 如果您現有的應用缺少 Podfile 配置檔案,請導航到您應用目錄的根目錄。使用 pod init 命令建立 Podfile 檔案。

  2. 更新您的 Podfile 配置檔案。

    1. platform 宣告之後新增以下行。

      MyApp/Podfile
      ruby
      flutter_application_path = '../my_flutter'
      load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
      
    2. 對於需要嵌入 Flutter 的每個 Podfile target,請呼叫 install_all_flutter_pods(flutter_application_path) 方法。在之前的步驟中的設定之後新增這些呼叫。

      MyApp/Podfile
      ruby
      target 'MyApp' do
        install_all_flutter_pods(flutter_application_path)
      end
      
    3. Podfilepost_install 塊中,呼叫 flutter_post_install(installer)。此塊應是 Podfile 配置檔案的最後一個塊。

      MyApp/Podfile
      ruby
      post_install do |installer|
        flutter_post_install(installer) if defined?(flutter_post_install)
      end
      

要檢視示例 Podfile,請參閱此 Flutter Podfile 示例

嵌入您的框架

#

在構建時,Xcode 會將您的 Dart 程式碼、每個 Flutter 外掛和 Flutter 引擎打包到各自的 *.xcframework 包中。CocoaPod 的 podhelper.rb 指令碼然後將這些 *.xcframework 包嵌入到您的專案中。

  • Flutter.xcframework 包含 Flutter 引擎。
  • App.xcframework 包含此專案的編譯後的 Dart 程式碼。
  • <plugin>.xcframework 包含一個 Flutter 外掛。

要將 Flutter 引擎、您的 Dart 程式碼和您的 Flutter 外掛嵌入到您的 iOS 應用中,請完成以下步驟。

  1. 重新整理您的 Flutter 外掛。

    如果您更改了 pubspec.yaml 檔案中的 Flutter 依賴項,請在您的 Flutter 模組目錄中執行 flutter pub get。這將重新整理 podhelper.rb 指令碼讀取的外掛列表。

    flutter pub get
    
  2. 使用 CocoaPods 嵌入外掛和框架。

    1. 導航到您的 iOS 應用專案,位於 /path/to/MyApp/MyApp

    2. 使用 pod install 命令。

      pod install
      

    您的 iOS 應用的 DebugRelease 構建配置嵌入了與該構建模式對應的 Flutter 元件

  3. 構建專案。

    1. 在 Xcode 中開啟 MyApp.xcworkspace

      驗證您是否正在開啟 MyApp.xcworkspace 而不是開啟 MyApp.xcodeproj.xcworkspace 檔案具有 CocoaPod 依賴項,而 .xcodeproj 沒有。

    2. 選擇 Product > Build 或按 Cmd + B

設定 LLDB Init 檔案

#
  1. 生成 Flutter LLDB 檔案。

    1. 在您的 flutter 應用中,執行以下命令
    flutter build ios --config-only
    

    這將在 .ios/Flutter/ephemeral 目錄中生成 LLDB 檔案。

  2. 設定 LLDB Init 檔案。

    1. 轉到 Product > Scheme > Edit Scheme

    2. 在左側邊欄中選擇 Run 部分。

    3. 使用與您在 更新您的 Podfile 部分中放入 Flutter 應用中的相同相對路徑設定 LLDB Init File

      $(SRCROOT)/../my_flutter/.ios/Flutter/ephemeral/flutter_lldbinit
      

      如果您的方案已經具有 LLDB Init File,您可以將 Flutter 的 LLDB 檔案新增到其中。Flutter LLDB Init 檔案的路徑必須相對於您的專案的 LLDB Init 檔案的位置。

      例如,如果您的 LLDB 檔案位於 /path/to/MyApp/.lldbinit,您將新增以下內容

      command source --relative-to-command-file "../my_flutter/.ios/Flutter/ephemeral/flutter_lldbinit"
      

在 Xcode 中連結和嵌入框架

#

方法

#

在第二種方法中,編輯您現有的 Xcode 專案,生成必要的框架,並將它們嵌入到您的應用中。Flutter 為 Flutter 本身、您編譯的 Dart 程式碼以及每個 Flutter 外掛生成 iOS 框架。嵌入這些框架並更新您現有的應用程式的構建設定。

要求

#

此方法不需要額外的軟體或硬體要求。在以下用例中使用此方法

  • 您的團隊成員無法安裝 Flutter SDK 和 CocoaPods
  • 您不想在現有的 iOS 應用中使用 CocoaPods 作為依賴項管理器

侷限性

#

Flutter 無法處理 具有 xcframework 的常見依賴項。如果您同時使用主機應用和 Flutter 模組的外掛定義相同的 pod 依賴項,並且使用此選項整合 Flutter 模組,則會發生錯誤。這些錯誤包括類似 Multiple commands produce 'CommonDependency.framework' 的問題。

要解決此問題,請將 Flutter 模組的每個外掛源連結到主機應用的 Podfile 中的其 podspec 檔案中。連結源而不是外掛的 xcframework 框架。下一節解釋瞭如何 生成該框架

為了防止由於公共依賴項存在而發生的錯誤,請使用 flutter build ios-framework 命令,並加上 --no-plugins 標誌。

示例專案結構

#

以下示例假定您希望將框架生成到 /path/to/MyApp/Flutter/

flutter build ios-framework --output=/path/to/MyApp/Flutter/

每次更改 Flutter 模組中的程式碼時,都必須執行此命令。

生成的專案結構應類似於以下目錄樹。

  • /path/to/MyApp/
    • Flutter/
      • Debug/
        • Flutter.xcframework
        • App.xcframework
        • FlutterPluginRegistrant.xcframework(如果您有帶有 iOS 平臺程式碼的外掛)
        • example_plugin.xcframework(每個外掛一個框架檔案)
      • Profile/
        • Flutter.xcframework
        • App.xcframework
        • FlutterPluginRegistrant.xcframework
        • example_plugin.xcframework
      • Release/
        • Flutter.xcframework
        • App.xcframework
        • FlutterPluginRegistrant.xcframework
        • example_plugin.xcframework

步驟

#

您將生成的框架連結、嵌入或同時進行連結和嵌入到您現有的 Xcode 應用中,取決於框架的型別。

Flutter 外掛可能會生成 靜態或動態框架。連結靜態框架,切勿嵌入它們

如果您將靜態框架嵌入到您的 iOS 應用中,您將無法將該應用釋出到 App Store。釋出會因 Found an unexpected Mach-O header code 存檔錯誤而失敗。

#

要連結必要的框架,請按照以下步驟操作。

  1. 選擇要連結的框架。

    1. 專案導航器 中,單擊您的專案。

    2. 單擊 構建階段 選項卡。

    3. 展開 連結二進位制檔案與庫

      Expand the **Link Binary With Libraries** build phase in Xcode

      在 Xcode 中展開 連結二進位制檔案與庫 構建階段

    4. 單擊 + (加號)。

    5. 單擊 新增其他...,然後單擊 新增檔案...

    6. 選擇要新增的框架和庫: 對話方塊中,導航到 /path/to/MyApp/Flutter/Release/ 目錄。

    7. 命令單擊該目錄中的框架,然後單擊 開啟

      Choose frameworks to link from the **Choose frameworks and libraries to add:** dialog box in Xcode

      從 Xcode 的 選擇要新增的框架和庫: 對話方塊中選擇要連結的框架

  2. 更新庫的路徑以考慮構建模式。

    1. 啟動 Finder。

    2. 導航到 /path/to/MyApp/ 目錄。

    3. 右鍵單擊 MyApp.xcodeproj 並選擇 顯示包內容

    4. 使用 Xcode 開啟 project.pbxproj。該檔案在 Xcode 的文字編輯器中開啟。這也會鎖定 專案導航器,直到您關閉文字編輯器。

      The `project-pbxproj` file open in the Xcode text editor

      Xcode 文字編輯器中開啟的 project-pbxproj 檔案

    5. /* Begin PBXFileReference section */ 中找到類似於以下文字的行。

      312885572C1A441C009F74FF /* Flutter.xcframework */ = {
        isa = PBXFileReference;
        expectedSignature = "AppleDeveloperProgram:S8QB4VV633:FLUTTER.IO LLC";
        lastKnownFileType = wrapper.xcframework;
        name = Flutter.xcframework;
        path = Flutter/Release/Flutter.xcframework;
        sourceTree = "<group>";
      };
      312885582C1A441C009F74FF /* App.xcframework */ = {
        isa = PBXFileReference;
        lastKnownFileType = wrapper.xcframework;
        name = App.xcframework;
        path = Flutter/Release/App.xcframework;
        sourceTree = "<group>";
      };
      
    6. 更改上一步中突出顯示的 Release 文字,並將其更改為 $(CONFIGURATION)。同時用引號將路徑括起來。

      312885572C1A441C009F74FF /* Flutter.xcframework */ = {
        isa = PBXFileReference;
        expectedSignature = "AppleDeveloperProgram:S8QB4VV633:FLUTTER.IO LLC";
        lastKnownFileType = wrapper.xcframework;
        name = Flutter.xcframework;
        path = "Flutter/$(CONFIGURATION)/Flutter.xcframework";
        sourceTree = "<group>";
      };
      312885582C1A441C009F74FF /* App.xcframework */ = {
        isa = PBXFileReference;
        lastKnownFileType = wrapper.xcframework;
        name = App.xcframework;
        path = "Flutter/$(CONFIGURATION)/App.xcframework";
        sourceTree = "<group>";
      };
      
  3. 更新搜尋路徑。

    1. 單擊 構建設定 選項卡。

    2. 導航到 搜尋路徑

    3. 雙擊 框架搜尋路徑 右側。

    4. 在組合框中,單擊 + (加號)。

    5. 鍵入 $(inherited) 並按 Enter

    6. 單擊 + (加號)。

    7. 鍵入 $(PROJECT_DIR)/Flutter/$(CONFIGURATION)/ 並按 Enter

      Update **Framework Search Paths** in Xcode

      更新 Xcode 中的 框架搜尋路徑

連結框架後,它們應顯示在目標 常規 設定的 框架、庫和嵌入內容 部分中。

嵌入動態框架
#

要嵌入您的動態框架,請完成以下步驟。

  1. 導航到 常規 > 框架、庫和嵌入內容

  2. 單擊您的每個動態框架,然後選擇 嵌入 & 簽名

    Select **Embed & Sign** for each of your frameworks in Xcode

    在 Xcode 中為您的每個框架選擇 嵌入 & 簽名

    不要包含任何靜態框架,包括 FlutterPluginRegistrant.xcframework

  3. 單擊 構建階段 選項卡。

  4. 展開 嵌入框架。您的動態框架應顯示在該部分中。

    The expanded **Embed Frameworks** build phase in Xcode

    Xcode 中展開的 嵌入框架 構建階段

  5. 構建專案。

    1. 在 Xcode 中開啟 MyApp.xcworkspace

      驗證您是否正在開啟 MyApp.xcworkspace 而不是開啟 MyApp.xcodeproj.xcworkspace 檔案具有 CocoaPod 依賴項,而 .xcodeproj 沒有。

    2. 選擇 產品 > 構建 或按 Cmd + B

設定 LLDB Init 檔案

#
  1. 生成 Flutter LLDB 檔案。

    1. 在您的 Flutter 應用程式中,如果您尚未這樣做,請重新執行 flutter build ios-framework
    flutter build ios-framework --output=/path/to/MyApp/Flutter/
    

    這將在 /path/to/MyApp/Flutter/ 目錄中生成 LLDB 檔案。

  2. 設定 LLDB Init 檔案。

    1. 轉到 Product > Scheme > Edit Scheme

    2. 在左側邊欄中選擇 Run 部分。

    3. LLDB 初始化檔案 設定為以下內容

      $(PROJECT_DIR)/Flutter/flutter_lldbinit
      

      如果您的方案已經具有 LLDB Init File,您可以將 Flutter 的 LLDB 檔案新增到其中。Flutter LLDB Init 檔案的路徑必須相對於您的專案的 LLDB Init 檔案的位置。

      例如,如果您的 LLDB 檔案位於 /path/to/MyApp/.lldbinit,您將新增以下內容

      command source --relative-to-command-file "Flutter/flutter_lldbinit"
      

在 Xcode 和 Flutter 框架中使用框架作為 podspec

#

方法

#

此方法生成 Flutter 作為 CocoaPods podspec,而不是將大型 Flutter.xcframework 分發給其他開發人員、機器或持續整合系統。Flutter 仍然為您的編譯後的 Dart 程式碼以及您的每個 Flutter 外掛生成 iOS 框架。嵌入這些框架並更新您現有應用程式的構建設定。

要求

#

此方法不需要額外的軟體或硬體要求。在以下用例中使用此方法

  • 您的團隊成員無法安裝 Flutter SDK 和 CocoaPods
  • 您不想在現有的 iOS 應用中使用 CocoaPods 作為依賴項管理器

侷限性

#

Flutter 無法處理 具有 xcframework 的常見依賴項。如果您同時使用主機應用和 Flutter 模組的外掛定義相同的 pod 依賴項,並且使用此選項整合 Flutter 模組,則會發生錯誤。這些錯誤包括類似 Multiple commands produce 'CommonDependency.framework' 的問題。

要解決此問題,請將 Flutter 模組的每個外掛源連結到主機應用的 Podfile 中的其 podspec 檔案中。連結源而不是外掛的 xcframework 框架。下一節解釋瞭如何 生成該框架

為了防止由於公共依賴項存在而發生的錯誤,請使用 flutter build ios-framework 命令,並加上 --no-plugins 標誌。

此方法僅適用於 betastable 釋出通道

示例專案結構

#

以下示例假定您希望將框架生成到 /path/to/MyApp/Flutter/

flutter build ios-framework --output=/path/to/MyApp/Flutter/

每次更改 Flutter 模組中的程式碼時,都必須執行此命令。

生成的專案結構應類似於以下目錄樹。

  • /path/to/MyApp/
    • Flutter/
      • Debug/
        • Flutter.xcframework
        • App.xcframework
        • FlutterPluginRegistrant.xcframework(如果您有帶有 iOS 平臺程式碼的外掛)
        • example_plugin.xcframework(每個外掛一個框架檔案)
      • Profile/
        • Flutter.xcframework
        • App.xcframework
        • FlutterPluginRegistrant.xcframework
        • example_plugin.xcframework
      • Release/
        • Flutter.xcframework
        • App.xcframework
        • FlutterPluginRegistrant.xcframework
        • example_plugin.xcframework

將 Flutter 引擎新增到您的 Podfile

#

可以使用 CocoaPods 的宿主應用將 Flutter 引擎新增到其 Podfile。

MyApp/Podfile
ruby
pod 'Flutter', :podspec => '/path/to/MyApp/Flutter/[build mode]/Flutter.podspec'
#

Flutter 外掛可能會生成 靜態或動態框架。連結靜態框架,切勿嵌入它們

如果您將靜態框架嵌入到您的 iOS 應用中,您將無法將該應用釋出到 App Store。釋出會因 Found an unexpected Mach-O header code 存檔錯誤而失敗。

#

要連結必要的框架,請按照以下步驟操作。

  1. 選擇要連結的框架。

    1. 專案導航器 中,單擊您的專案。

    2. 單擊 構建階段 選項卡。

    3. 展開 連結二進位制檔案與庫

      Expand the **Link Binary With Libraries** build phase in Xcode

      在 Xcode 中展開 連結二進位制檔案與庫 構建階段

    4. 單擊 + (加號)。

    5. 單擊 新增其他...,然後單擊 新增檔案...

    6. 選擇要新增的框架和庫: 對話方塊中,導航到 /path/to/MyApp/Flutter/Release/ 目錄。

    7. 命令單擊該目錄中的框架,然後單擊 開啟

      Choose frameworks to link from the **Choose frameworks and libraries to add:** dialog box in Xcode

      從 Xcode 的 選擇要新增的框架和庫: 對話方塊中選擇要連結的框架

  2. 更新庫的路徑以考慮構建模式。

    1. 啟動 Finder。

    2. 導航到 /path/to/MyApp/ 目錄。

    3. 右鍵單擊 MyApp.xcodeproj 並選擇 顯示包內容

    4. 使用 Xcode 開啟 project.pbxproj。該檔案在 Xcode 的文字編輯器中開啟。這也會鎖定 專案導航器,直到您關閉文字編輯器。

      The `project-pbxproj` file open in the Xcode text editor

      Xcode 文字編輯器中開啟的 project-pbxproj 檔案

    5. /* Begin PBXFileReference section */ 中找到類似於以下文字的行。

      312885572C1A441C009F74FF /* Flutter.xcframework */ = {
        isa = PBXFileReference;
        expectedSignature = "AppleDeveloperProgram:S8QB4VV633:FLUTTER.IO LLC";
        lastKnownFileType = wrapper.xcframework;
        name = Flutter.xcframework;
        path = Flutter/Release/Flutter.xcframework;
        sourceTree = "<group>";
      };
      312885582C1A441C009F74FF /* App.xcframework */ = {
        isa = PBXFileReference;
        lastKnownFileType = wrapper.xcframework;
        name = App.xcframework;
        path = Flutter/Release/App.xcframework;
        sourceTree = "<group>";
      };
      
    6. 更改上一步中突出顯示的 Release 文字,並將其更改為 $(CONFIGURATION)。同時用引號將路徑括起來。

      312885572C1A441C009F74FF /* Flutter.xcframework */ = {
        isa = PBXFileReference;
        expectedSignature = "AppleDeveloperProgram:S8QB4VV633:FLUTTER.IO LLC";
        lastKnownFileType = wrapper.xcframework;
        name = Flutter.xcframework;
        path = "Flutter/$(CONFIGURATION)/Flutter.xcframework";
        sourceTree = "<group>";
      };
      312885582C1A441C009F74FF /* App.xcframework */ = {
        isa = PBXFileReference;
        lastKnownFileType = wrapper.xcframework;
        name = App.xcframework;
        path = "Flutter/$(CONFIGURATION)/App.xcframework";
        sourceTree = "<group>";
      };
      
  3. 更新搜尋路徑。

    1. 單擊 構建設定 選項卡。

    2. 導航到 搜尋路徑

    3. 雙擊 框架搜尋路徑 右側。

    4. 在組合框中,單擊 + (加號)。

    5. 鍵入 $(inherited) 並按 Enter

    6. 單擊 + (加號)。

    7. 鍵入 $(PROJECT_DIR)/Flutter/$(CONFIGURATION)/ 並按 Enter

      Update **Framework Search Paths** in Xcode

      更新 Xcode 中的 框架搜尋路徑

連結框架後,它們應顯示在目標 常規 設定的 框架、庫和嵌入內容 部分中。

嵌入動態框架
#

要嵌入您的動態框架,請完成以下步驟。

  1. 導航到 常規 > 框架、庫和嵌入內容

  2. 單擊您的每個動態框架,然後選擇 嵌入 & 簽名

    Select **Embed & Sign** for each of your frameworks in Xcode

    在 Xcode 中為您的每個框架選擇 嵌入 & 簽名

    不要包含任何靜態框架,包括 FlutterPluginRegistrant.xcframework

  3. 單擊 構建階段 選項卡。

  4. 展開 嵌入框架。您的動態框架應顯示在該部分中。

    The expanded **Embed Frameworks** build phase in Xcode

    Xcode 中展開的 嵌入框架 構建階段

  5. 構建專案。

    1. 在 Xcode 中開啟 MyApp.xcworkspace

      驗證您是否正在開啟 MyApp.xcworkspace 而不是開啟 MyApp.xcodeproj.xcworkspace 檔案具有 CocoaPod 依賴項,而 .xcodeproj 沒有。

    2. 選擇 產品 > 構建 或按 Cmd + B

設定 LLDB Init 檔案

#
  1. 生成 Flutter LLDB 檔案。

    1. 在您的 Flutter 應用程式中,如果您尚未這樣做,請重新執行 flutter build ios-framework
    flutter build ios-framework --output=/path/to/MyApp/Flutter/
    

    這將在 /path/to/MyApp/Flutter/ 目錄中生成 LLDB 檔案。

  2. 設定 LLDB Init 檔案。

    1. 轉到 Product > Scheme > Edit Scheme

    2. 在左側邊欄中選擇 Run 部分。

    3. LLDB 初始化檔案 設定為以下內容

      $(PROJECT_DIR)/Flutter/flutter_lldbinit
      

      如果您的方案已經具有 LLDB Init File,您可以將 Flutter 的 LLDB 檔案新增到其中。Flutter LLDB Init 檔案的路徑必須相對於您的專案的 LLDB Init 檔案的位置。

      例如,如果您的 LLDB 檔案位於 /path/to/MyApp/.lldbinit,您將新增以下內容

      command source --relative-to-command-file "Flutter/flutter_lldbinit"
      

設定本地網路隱私許可權

#

在 iOS 14 及更高版本上,在 iOS 應用的 Debug 版本中啟用 Dart multicast DNS 服務。這增加了使用 flutter attach 進行 除錯功能,例如熱過載和 DevTools

要僅在應用的 Debug 版本中設定本地網路隱私許可權,請為每個構建配置建立一個單獨的 Info.plist。SwiftUI 專案預設情況下沒有 Info.plist 檔案。如果您需要建立屬性列表,可以透過 Xcode 或文字編輯器來完成。以下說明假定預設的 DebugRelease。根據您的應用的構建配置調整名稱。

  1. 建立一個新的屬性列表。

    1. 在 Xcode 中開啟您的專案。

    2. 專案導航器 中,單擊專案名稱。

    3. 目標 列表中,單擊您的應用。

    4. 單擊 資訊 選項卡。

    5. 展開 自定義 iOS 目標屬性

    6. 右鍵單擊列表並選擇 新增行

    7. 從下拉選單中選擇 Bonjour 服務。這將在專案目錄中建立一個名為 Info 的新屬性列表。這在 Finder 中顯示為 Info.plist

  2. Info.plist 重新命名為 Info-Debug.plist

    1. 單擊專案列表左側的 資訊 檔案。

    2. 在右側的 身份和型別 面板中,將 名稱Info.plist 更改為 Info-Debug.plist

  3. 建立 Release 屬性列表。

    1. 專案導航器 中,單擊 Info-Debug.plist

    2. 選擇 檔案 > 複製...。您也可以按 Cmd + Shift + S

    3. 在對話方塊中,將 儲存為: 欄位設定為 Info-Release.plist,然後單擊 儲存

  4. 將必要的屬性新增到 Debug 屬性列表中。

    1. 專案導航器 中,單擊 Info-Debug.plist

    2. 將字串值 _dartVmService._tcp 新增到 Bonjour 服務 陣列。

    3. (可選) 要設定您想要的自定義許可權對話方塊文字,請新增鍵 隱私 - 本地網路使用說明

      The `Info-Debug` property list with the **Bonjour Services** and **Privacy - Local Network Usage Description** keys added

      添加了 Bonjour 服務隱私 - 本地網路使用說明 鍵的 Info-Debug 屬性列表

  5. 設定目標以針對不同的構建模式使用不同的屬性列表。

    1. 專案導航器 中,單擊您的專案。

    2. 單擊 構建設定 選項卡。

    3. 單擊 全部組合 子選項卡。

    4. 在搜尋框中鍵入 plist。這將限制設定到包含屬性列表的設定。

    5. 向下滾動列表,直到看到 打包

    6. 單擊 資訊.plist 檔案 設定。

    7. 資訊.plist 檔案 值從 path/to/Info.plist 更改為 path/to/Info-$(CONFIGURATION).plist

      Updating the `Info.plist` build setting to use build mode-specific property lists

      更新 資訊.plist 構建設定以使用特定於構建模式的屬性列表

      這在 Debug 中解析為路徑 Info-Debug.plist,在 Release 中解析為 Info-Release.plist

      The updated **Info.plist File** build setting displaying the configuration variations

      更新的 資訊.plist 檔案 構建設定顯示配置變化

  6. 構建階段 中刪除 Release 屬性列表。

    1. 專案導航器 中,單擊您的專案。

    2. 單擊 構建階段 選項卡。

    3. 展開 複製捆綁資源

    4. 如果此列表中包含 Info-Release.plist,請單擊它,然後單擊其下方的 - (減號) 以將其從資源列表中刪除。

      The **Copy Bundle** build phase displaying the **Info-Release.plist** setting. Remove this setting.

      顯示 Info-Release.plist 設定的 複製捆綁包 構建階段。刪除此設定。

  7. 您的 Debug 應用載入的第一個螢幕會提示本地網路許可權。

    單擊 確定

    (可選) 要在應用載入之前授予許可權,請啟用 設定 > 隱私 > 本地網路 > 您的應用

緩解 Apple Silicon Mac 上的已知問題

#

執行 Apple Silicon 的 Mac 上,宿主應用為 arm64 模擬器構建。雖然 Flutter 支援 arm64 模擬器,但某些外掛可能不支援。如果您使用其中一個外掛,您可能會看到類似於 Undefined symbols for architecture arm64 的編譯錯誤。如果發生這種情況,請從宿主應用的模擬器架構中排除 arm64

  1. 專案導航器 中,單擊您的專案。

  2. 單擊 構建設定 選項卡。

  3. 單擊 全部組合 子選項卡。

  4. 架構 下,單擊 排除的架構

  5. 展開以檢視可用的構建配置。

  6. 單擊 Debug

  7. 單擊 + (加號)。

  8. 選擇 iOS 模擬器

  9. 雙擊 任何 iOS 模擬器 SDK 的值列。

  10. 單擊 + (加號)。

  11. Debug > 任何 iOS 模擬器 SDK 對話方塊中鍵入 arm64

    Add `arm64` as an excluded architecture for your app

    arm64 新增為應用的排除架構

  12. Esc 關閉此對話方塊。

  13. Release 構建模式重複這些步驟。

  14. 對任何 iOS 單元測試目標重複操作。

下一步

#

現在您可以 將 Flutter 螢幕 新增到您現有的 iOS 應用中。