跳至主要內容

將 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 應用程式的框架和外掛程式,但以 CocoaPods podspec 的形式發佈 Flutter 引擎。提供一種替代方案來發佈大型 Flutter 引擎 (Flutter.xcframework) 程式庫。

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

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

開發系統需求

#

您的開發環境必須符合Flutter 的 macOS 系統需求,並且已安裝 Xcode。Flutter 支援 Xcode 15 或更新版本和 CocoaPods 1.16 或更新版本。

建立 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 建置框架或將模組嵌入到您現有的應用程式中。

在你的 iOS 應用程式中嵌入 Flutter 模組

#

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

您可以在模擬器或真實裝置上以 Debug 模式執行,並在真實裝置上以 Release 模式執行。

使用 CocoaPods 和 Flutter SDK

#

方法

#

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

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

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

觀看影片

#

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


將 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. 瀏覽至 /path/to/MyApp/MyApp 的 iOS 應用程式專案。

    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

在 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 (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. 專案導覽器 中,點擊您的專案。

    2. 點擊 建置階段 標籤。

    3. 展開 連結二進制與函式庫

      Expand the Link Binary With Libraries build phase in Xcode
      在 Xcode 中展開 連結二進制與函式庫 建置階段。

    4. 點擊 + (加號)。

    5. 點擊 加入其他... 然後 加入檔案...

    6. 選擇要加入的框架和函式庫: 對話框中,導覽至 /path/to/MyApp/Flutter/Release/ 目錄。

    7. 按住 Command 鍵並點擊該目錄中的框架,然後點擊 開啟

      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  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. 點擊 建置設定 標籤。

    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

將框架用於 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 (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. 專案導覽器 中,點擊您的專案。

    2. 點擊 建置階段 標籤。

    3. 展開 連結二進制與函式庫

      Expand the Link Binary With Libraries build phase in Xcode
      在 Xcode 中展開 連結二進制與函式庫 建置階段。

    4. 點擊 + (加號)。

    5. 點擊 加入其他... 然後 加入檔案...

    6. 選擇要加入的框架和函式庫: 對話框中,導覽至 /path/to/MyApp/Flutter/Release/ 目錄。

    7. 按住 Command 鍵並點擊該目錄中的框架,然後點擊 開啟

      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  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. 點擊 建置設定 標籤。

    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

設定本地網路隱私權限

#

在 iOS 14 和更新版本上,在 iOS 應用程式的 Debug 版本中啟用 Dart 多點傳送 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. 建立發佈屬性清單。

    1. 專案導覽器 中,點擊 Info-Debug.plist

    2. 選取 檔案 > 重複...
      您也可以按下 Cmd + Shift + S

    3. 在對話框中,將 另存為: 欄位設定為 Info-Release.plist,然後點擊 儲存

  4. 將必要屬性新增至 Debug 屬性清單。

    1. 專案導覽器 中,點擊 Info-Debug.plist

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

    3. (選用) 若要設定您想要的自訂權限對話文字,請新增金鑰 隱私權 - 本機網路使用描述

      The  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. 點擊 Info.plist 檔案 設定。

    7. Info.plist 檔案 值從 path/to/Info.plist 變更為 path/to/Info-$(CONFIGURATION).plist更新建置設定以使用特定建置模式的屬性清單
      更新 Info.plist 建置設定以使用特定建置模式的屬性清單

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

      The updated Info.plist File build setting displaying the
configuration variations
      更新的 Info.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 應用程式載入的第一個 Flutter 畫面會提示本機網路權限。

    點擊 確定

    (選用) 若要在應用程式載入之前授與權限,請啟用 設定 > 隱私權 > 本機網路 > 您的應用程式

減輕 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  as an excluded architecture for your app
    arm64 新增為您應用程式的排除架構

  12. 按下 Esc 以關閉此對話框。

  13. 針對 Release 建置模式重複這些步驟。

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

後續步驟

#

您現在可以將 Flutter 畫面新增至您現有的 iOS 應用程式。