跳到主內容

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

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

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

嵌入方法方法論優勢
使用 CocoaPods (推薦) 安裝並使用 Flutter SDK 和 CocoaPods。每次 Xcode 構建 iOS 應用時,Flutter 都會從原始碼編譯 flutter_module 將 Flutter 嵌入應用最簡單的方法。
使用 iOS 框架 為 Flutter 元件建立 iOS 框架,將其嵌入到您的 iOS 專案中,並更新現有應用的構建設定。 不需要每位開發人員都在其本地機器上安裝 Flutter SDK 和 CocoaPods。
使用 iOS 框架和 CocoaPods 在 Xcode 中為您的 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。您也可以在 VS CodeAndroid Studio/IntelliJ 中使用 Flutter 和 Dart 外掛執行該模組。該專案在嵌入到現有 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 模組。

這允許使用最新版本的 Flutter 模組進行快速迭代,而無需在 Xcode 之外執行額外的命令。

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

觀看影片

#

如果您透過觀看影片學習效果更好,此影片涵蓋了將 Flutter 新增到 iOS 應用的過程

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

要求

#

專案中工作的每位開發人員都必須安裝本地版本的 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. 導航到 /path/to/MyApp/MyApp 下的 iOS 應用專案。

    2. 使用 pod install 命令。

      pod install
      

    您的 iOS 應用的 Debug(除錯)Release(釋出)構建配置會嵌入 該構建模式對應的 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(LLDB 初始化檔案)

      $(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 框架。下一節將解釋如何 生成該框架

為防止出現公共依賴項導致的錯誤,請使用帶有 --no-plugins 標誌的 flutter build ios-framework

示例專案結構

#

以下示例假設您要將框架生成到 /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. 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. 按住 Cmd 點選該目錄中的框架,然後點選 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 `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. 點選 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

連結框架後,它們應該會顯示在目標 (target) 的 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 框架。下一節將解釋如何 生成該框架

為防止出現公共依賴項導致的錯誤,請使用帶有 --no-plugins 標誌的 flutter build ios-framework

此方法僅適用於 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. 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. 按住 Cmd 點選該目錄中的框架,然後點選 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 `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. 點選 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

連結框架後,它們應該會顯示在目標 (target) 的 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 應用的 Debug(除錯)版本中啟用 Dart 多播 DNS 服務。這使用 flutter attach 添加了 熱過載和 DevTools 等除錯功能

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

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

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

    2. Project Navigator 中,點選專案名稱。

    3. 從編輯器窗格的 Targets 列表中,點選您的應用。

    4. 點選 Info 選項卡。

    5. 展開 Custom iOS Target Properties(自定義 iOS 目標屬性)

    6. 右鍵點選列表並選擇 Add Row(新增行)

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

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

    1. 點選左側專案列表中的 Info 檔案。

    2. 在右側的 Identity and Type(標識和型別)面板中,將 Name(名稱)Info.plist 更改為 Info-Debug.plist

  3. 建立一個 Release 屬性列表。

    1. Project Navigator 中,點選 Info-Debug.plist

    2. 選擇 File > Duplicate...(複製...)。您也可以按 Cmd + Shift + S

    3. 在對話方塊中,將 Save As:(另存為:)欄位設定為 Info-Release.plist 並點選 Save(儲存)

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

    1. Project Navigator 中,點選 Info-Debug.plist

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

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

      The `Info-Debug` 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. 在搜尋框中輸入 plist。這會將設定限制為包含屬性列表的設定。

    5. 滾動列表,直到看到 Packaging

    6. 點選 Info.plist File 設定。

    7. Info.plist File 的值從 path/to/Info.plist 更改為 path/to/Info-$(CONFIGURATION).plist

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

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

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

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

      更新後的 Info.plist File 構建設定,顯示配置變體

  6. Build Phases 中移除 Release 屬性列表。

    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.

      顯示 Info-Release.plist 設定的 Copy Bundle 構建階段。移除此設定。

  7. 您的 Debug 應用載入的第一個 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(任何 iOS 模擬器 SDK)的值列。

  10. 點選 +(加號)。

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

    Add `arm64` as an excluded architecture for your app

    arm64 新增為您的應用的排除架構

  12. Esc 關閉此對話方塊。

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

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

下一步

#

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