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

嵌入方法方法優勢
使用 CocoaPods (推薦)安裝並使用 Flutter SDK 和 CocoaPods。每次 Xcode 構建 iOS 應用時,Flutter 都會從原始碼編譯 flutter_module將 Flutter 嵌入到您的應用程式中最簡單的方法。
使用 iOS 框架為 Flutter 元件建立 iOS 框架,將它們嵌入到您的 iOS 專案中,並更新您現有應用程式的構建設定。不需要每個開發人員都在其本地計算機上安裝 Flutter SDK 和 CocoaPods。
使用 iOS 框架和 CocoaPods將 Flutter 元件的框架嵌入到您的 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 模組後,您可以使用頁面頂部的表格中所述的方法將其嵌入。

您可以在模擬器或真實裝置上以除錯模式執行,在真實裝置上以釋出模式執行。

使用 CocoaPods 和 Flutter SDK

#

方法

#

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

這使得您無需在 Xcode 之外執行其他命令,即可快速迭代並使用最新版本的 Flutter 模組。

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

觀看影片

#

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

在新標籤頁中 YouTube 觀看:“如何將 Flutter 新增到現有 iOS 應用的分步指南”

要求

#

您專案中的每個開發人員都必須安裝本地版本的 Flutter SDK 和 CocoaPods。

示例專案結構

#

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

/path/to/MyApp
├── 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 目標,請新增對 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 應用的除錯釋出構建配置會嵌入相應構建模式的 Flutter 元件

  3. 構建專案。

    1. 在 Xcode 中開啟 MyApp.xcworkspace

      請確保您開啟的是 MyApp.xcworkspace 而不是 MyApp.xcodeproj.xcworkspace 檔案包含 CocoaPod 依賴項,而 .xcodeproj 不包含。

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

設定 LLDB 初始化檔案

#
  1. 生成 Flutter LLDB 檔案。

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

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

  2. 設定 LLDB 初始化檔案。

    1. 轉到 Product > Scheme > Edit Scheme

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

    3. 使用您在“更新您的 Podfile”部分在 Podfile 中設定的與您的 Flutter 應用程式相同的相對路徑來設定 LLDB Init File

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

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

      例如,如果您的 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 無法處理與 xcframeworks 的常見依賴項。如果宿主應用和 Flutter 模組的外掛都定義了相同的 pod 依賴項,並且您使用此選項整合 Flutter 模組,則會導致錯誤。這些錯誤包括 Multiple commands produce 'CommonDependency.framework' 之類的問題。

要解決此問題,請將 Flutter 模組中每個外掛的 podspec 檔案中的源連結到宿主應用的 Podfile。連結源而不是外掛的 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 (only if you have plugins with iOS platform code)
    │   └── example_plugin.xcframework (each plugin is a separate framework)
    ├── 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. Project Navigator 中,單擊您的專案。

    2. 單擊 Build Phases 選項卡。

    3. 展開 Link Binary With Libraries

      Expand the Link Binary With Libraries build phase in Xcode
      在 Xcode 中展開 Link Binary With Libraries 構建階段

    4. 單擊 + (加號)。

    5. 單擊 Add Other...,然後單擊 Add Files...

    6. Choose frameworks and libraries to add: 對話方塊中,導航到 /path/to/MyApp/Flutter/Release/ 目錄。

    7. 按住 Command 鍵並單擊該目錄中的框架,然後單擊 Open

      Choose frameworks to link from the Choose frameworks and
libraries to add: dialog box in Xcode
      在 Xcode 的 Choose frameworks and libraries to add: 對話方塊中選擇要連結的框架

  2. 更新庫的路徑以適應構建模式。

    1. 啟動 Finder。

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

    3. 右鍵單擊 MyApp.xcodeproj 並選擇 Show Package Contents

    4. 使用 Xcode 開啟 project.pbxproj。該檔案會在 Xcode 的文字編輯器中開啟。您關閉文字編輯器之前,這也會鎖定 Project Navigator

      The  file open in the Xcode text editor
      project-pbxproj 檔案在 Xcode 文字編輯器中開啟

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

      text
      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)。同時將路徑用引號括起來。

      text
      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. 單擊 Build Settings 選項卡。

    2. 導航到 Search Paths

    3. 雙擊 Framework Search Paths 右側的區域。

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

    5. 鍵入 $(inherited)。然後按 Enter

    6. 單擊 + (加號)。

    7. 鍵入 $(PROJECT_DIR)/Flutter/$(CONFIGURATION)/,然後按 Enter

      Update Framework Search Paths in Xcode
      在 Xcode 中更新 Framework Search Paths

連結框架後,它們應顯示在目標 General 設定的 Frameworks, Libraries, and Embedded Content 部分。

嵌入動態框架
#

要嵌入動態框架,請完成以下過程。

  1. 導航到 General > Frameworks, Libraries, and Embedded Content

  2. 單擊每個動態框架,然後選擇 Embed & Sign

    Select Embed  Sign for each of your frameworks in Xcode
    在 Xcode 中為每個框架選擇 Embed & Sign

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

  3. 單擊 Build Phases 選項卡。

  4. 展開 Embed Frameworks。您的動態框架應在該部分顯示。

    The expanded Embed Frameworks build phase in Xcode
    Xcode 中展開的 Embed Frameworks 構建階段

  5. 構建專案。

    1. 在 Xcode 中開啟 MyApp.xcworkspace

      請確保您開啟的是 MyApp.xcworkspace 而不是 MyApp.xcodeproj.xcworkspace 檔案包含 CocoaPod 依賴項,而 .xcodeproj 不包含。

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

設定 LLDB 初始化檔案

#
  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 初始化檔案。

    1. 轉到 Product > Scheme > Edit Scheme

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

    3. LLDB Init File 設定為以下內容

      $(PROJECT_DIR)/Flutter/flutter_lldbinit

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

      例如,如果您的 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 無法處理與 xcframeworks 的常見依賴項。如果宿主應用和 Flutter 模組的外掛都定義了相同的 pod 依賴項,並且您使用此選項整合 Flutter 模組,則會導致錯誤。這些錯誤包括 Multiple commands produce 'CommonDependency.framework' 之類的問題。

要解決此問題,請將 Flutter 模組中每個外掛的 podspec 檔案中的源連結到宿主應用的 Podfile。連結源而不是外掛的 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 (only if you have plugins with iOS platform code)
    │   └── example_plugin.xcframework (each plugin is a separate framework)
    ├── 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. Project Navigator 中,單擊您的專案。

    2. 單擊 Build Phases 選項卡。

    3. 展開 Link Binary With Libraries

      Expand the Link Binary With Libraries build phase in Xcode
      在 Xcode 中展開 Link Binary With Libraries 構建階段

    4. 單擊 + (加號)。

    5. 單擊 Add Other...,然後單擊 Add Files...

    6. Choose frameworks and libraries to add: 對話方塊中,導航到 /path/to/MyApp/Flutter/Release/ 目錄。

    7. 按住 Command 鍵並單擊該目錄中的框架,然後單擊 Open

      Choose frameworks to link from the Choose frameworks and
libraries to add: dialog box in Xcode
      在 Xcode 的 Choose frameworks and libraries to add: 對話方塊中選擇要連結的框架

  2. 更新庫的路徑以適應構建模式。

    1. 啟動 Finder。

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

    3. 右鍵單擊 MyApp.xcodeproj 並選擇 Show Package Contents

    4. 使用 Xcode 開啟 project.pbxproj。該檔案會在 Xcode 的文字編輯器中開啟。您關閉文字編輯器之前,這也會鎖定 Project Navigator

      The  file open in the Xcode text editor
      project-pbxproj 檔案在 Xcode 文字編輯器中開啟

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

      text
      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)。同時將路徑用引號括起來。

      text
      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. 單擊 Build Settings 選項卡。

    2. 導航到 Search Paths

    3. 雙擊 Framework Search Paths 右側的區域。

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

    5. 鍵入 $(inherited)。然後按 Enter

    6. 單擊 + (加號)。

    7. 鍵入 $(PROJECT_DIR)/Flutter/$(CONFIGURATION)/,然後按 Enter

      Update Framework Search Paths in Xcode
      在 Xcode 中更新 Framework Search Paths

連結框架後,它們應顯示在目標 General 設定的 Frameworks, Libraries, and Embedded Content 部分。

嵌入動態框架
#

要嵌入動態框架,請完成以下過程。

  1. 導航到 General > Frameworks, Libraries, and Embedded Content

  2. 單擊每個動態框架,然後選擇 Embed & Sign

    Select Embed  Sign for each of your frameworks in Xcode
    在 Xcode 中為每個框架選擇 Embed & Sign

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

  3. 單擊 Build Phases 選項卡。

  4. 展開 Embed Frameworks。您的動態框架應在該部分顯示。

    The expanded Embed Frameworks build phase in Xcode
    Xcode 中展開的 Embed Frameworks 構建階段

  5. 構建專案。

    1. 在 Xcode 中開啟 MyApp.xcworkspace

      請確保您開啟的是 MyApp.xcworkspace 而不是 MyApp.xcodeproj.xcworkspace 檔案包含 CocoaPod 依賴項,而 .xcodeproj 不包含。

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

設定 LLDB 初始化檔案

#
  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 初始化檔案。

    1. 轉到 Product > Scheme > Edit Scheme

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

    3. LLDB Init File 設定為以下內容

      $(PROJECT_DIR)/Flutter/flutter_lldbinit

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

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

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

設定本地網路隱私許可權

#

在 iOS 14 及更高版本上,在 iOS 應用的除錯版本中啟用 Dart 多播 DNS 服務。這會新增除錯功能,例如熱過載和 DevTools,透過 flutter attach 實現。

要僅在除錯版本中設定本地網路隱私許可權,請為每個構建配置建立一個單獨的 Info.plist。SwiftUI 專案啟動時沒有 Info.plist 檔案。如果需要建立屬性列表,可以透過 Xcode 或文字編輯器進行。以下說明假定使用預設的除錯釋出配置。根據您應用的構建配置,按需調整名稱。

  1. 建立新的屬性列表。

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

    2. Project Navigator 中,單擊專案名稱。

    3. 在 Editor 面板的 Targets 列表中,單擊您的應用。

    4. 單擊 Info 選項卡。

    5. 展開 Custom iOS Target Properties

    6. 右鍵單擊列表並選擇 Add Row

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

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

    1. 在左側的專案列表中單擊 Info 檔案。

    2. 在右側的 Identity and Type 面板中,將 NameInfo.plist 更改為 Info-Debug.plist

  3. 建立釋出屬性列表。

    1. Project Navigator 中,單擊 Info-Debug.plist

    2. 選擇 File > Duplicate...
      您也可以按 Cmd + Shift + S

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

  4. 除錯屬性列表新增必要的屬性。

    1. Project Navigator 中,單擊 Info-Debug.plist

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

    3. (可選)要設定您想要的自定義許可權對話方塊文字,請新增 Privacy - Local Network Usage Description 鍵。

      The  property list with the Bonjour Services
and Privacy - Local Network Usage Description keys added
      添加了 Bonjour ServicesPrivacy - Local Network Usage Description 鍵的 Info-Debug 屬性列表

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

    1. Project Navigator 中,單擊您的專案。

    2. 單擊 Build Settings 選項卡。

    3. 單擊 AllCombined 子選項卡。

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

    5. 滾動列表直到看到 Packaging

    6. 單擊 Info.plist File 設定。

    7. Info.plist File 的值從 path/to/Info.plist 更改為 path/to/Info-$(CONFIGURATION).plistUpdating the build setting to use build mode-specific property lists
      更新 Info.plist 構建設定以使用特定於構建模式的屬性列表

      這會在除錯模式下解析為路徑 Info-Debug.plist,在釋出模式下解析為 Info-Release.plist

      The updated Info.plist File build setting displaying the
configuration variations
      顯示配置變化的已更新 Info.plist File 構建設定

  6. 構建階段中移除釋出屬性列表。

    1. Project Navigator 中,單擊您的專案。

    2. 單擊 Build Phases 選項卡。

    3. 展開 Copy Bundle Resources

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

      The Copy Bundle build phase displaying the
Info-Release.plist setting. Remove this setting.
      Copy Bundle 構建階段顯示 Info-Release.plist 設定。刪除此設定。

  7. 您除錯應用的第一個 Flutter 螢幕會提示本地網路許可權。

    單擊 OK

    (可選)要在應用載入前授予許可權,請在 Settings > Privacy > Local Network > Your App 中啟用。

緩解 Apple Silicon Mac 的已知問題

#

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

  1. Project Navigator 中,單擊您的專案。

  2. 單擊 Build Settings 選項卡。

  3. 單擊 AllCombined 子選項卡。

  4. Architectures 下,單擊 Excluded Architectures

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

  6. 單擊 Debug

  7. 單擊 + (加號)。

  8. 選擇 iOS Simulator

  9. 雙擊 Any iOS Simulator SDK 的值列。

  10. 單擊 + (加號)。

  11. Debug > Any iOS Simulator SDK 對話方塊中鍵入 arm64

    Add  as an excluded architecture for your app
    arm64 新增為應用的排除架構

  12. Esc 關閉此對話方塊。

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

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

下一步

#

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