您可以將 Flutter UI 元件(Widgets)以嵌入式 framework 的方式,逐步加入到現有的 iOS 應用程式中。 要在現有應用程式中嵌入 Flutter,請考慮下列三種方法之一。

嵌入方式方法說明優點
使用 CocoaPods (推薦)安裝並使用 Flutter SDK 與 CocoaPods。每次 Xcode 建置 iOS 應用程式時,Flutter 會從原始碼編譯 flutter_module將 Flutter 嵌入應用程式最簡單的方法。
使用iOS frameworks為 Flutter 元件建立 iOS framework,將其嵌入到您的 iOS 專案,並更新現有應用程式的建置設定。不需要每位開發者都在本機安裝 Flutter SDK 與 CocoaPods。
同時使用 iOS frameworks 與 CocoaPods在 Xcode 中嵌入 iOS 應用程式與外掛的 framework,但將 Flutter engine 以 CocoaPods podspec 方式發佈。提供一種替代方案,避免直接分發大型 Flutter engine(Flutter.xcframework)函式庫。

當您將 Flutter 加入現有的 iOS 應用程式時,會增加您的 iOS 應用程式的體積

若需以 UIKit 建立的應用程式範例,請參考 add_to_app code samples 中的 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 workspace, 你可以在其中執行你的模組獨立版本。 這個包裝專案負責啟動你的 Flutter 程式碼。 它包含協助腳本,方便你建置 frameworks 或 透過 CocoaPods 將模組嵌入到你現有的應用程式中。

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

#

當你開發好 Flutter 模組後, 可以使用本頁頂端表格中描述的方法進行嵌入。

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

Use CocoaPods and the Flutter SDK

#

Approach

#

This first method uses CocoaPods to embed the Flutter modules. CocoaPods manages dependencies for Swift projects, including Flutter code and plugins. Each time Xcode builds the app, CocoaPods embeds the Flutter modules.

This allows rapid iteration with the most up-to-date version of your Flutter module without running additional commands outside of Xcode.

To learn more about CocoaPods, consult the CocoaPods getting started guide.

Watch the video

#

If watching a video helps you learn, this video covers adding Flutter to an iOS app:

Watch on YouTube in a new tab: "Step by step on how to add Flutter to an existing iOS app"

Requirements

#

Every developer working on your project must have a local version of the Flutter SDK and CocoaPods installed.

Example project structure

#

This section assumes that your existing app and the Flutter module reside in sibling directories. If you have a different directory structure, adjust the relative paths. The example directory structure resembles the following:

/path/to/MyApp
├── my_flutter/
│   └── .ios/
│       └── Flutter/
│         └── podhelper.rb
└── MyApp/
    └── Podfile

Update your Podfile

#

Add your Flutter modules to your Podfile configuration file. This section presumes you called your Swift app MyApp.

  1. (Optional) If your existing app lacks a Podfile config file, navigate to the root of your app directory. Use the pod init command to create the Podfile file.

  2. Update your Podfile config file.

    1. Add the following lines after the platform declaration.

      MyApp/Podfile
      ruby
      flutter_application_path = '../my_flutter'
      load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
    2. For each Podfile target that needs to embed Flutter, add a call to the install_all_flutter_pods(flutter_application_path) method. Add these calls after the settings in the previous step.

      MyApp/Podfile
      ruby
      target 'MyApp' do
        install_all_flutter_pods(flutter_application_path)
      end
    3. In the Podfile's post_install block, add a call to flutter_post_install(installer). This block should be the last block in the Podfile config file.

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

To review an example Podfile, consult this Flutter Podfile sample.

Embed your frameworks

#

At build time, Xcode packages your Dart code, each Flutter plugin, and the Flutter engine into their own *.xcframework bundles. CocoaPod's podhelper.rb script then embeds these *.xcframework bundles into your project.

  • Flutter.xcframework contains the Flutter engine.
  • App.xcframework contains the compiled Dart code for this project.
  • <plugin>.xcframework contains one Flutter plugin.

To embed the Flutter engine, your Dart code, and your Flutter plugins into your iOS app, complete the following procedure.

  1. Refresh your Flutter plugins.

    If you change the Flutter dependencies in the pubspec.yaml file, run flutter pub get in your Flutter module directory. This refreshes the list of plugins that the podhelper.rb script reads.

    flutter pub get
  2. Embed the plugins and frameworks with CocoaPods.

    1. Navigate to your iOS app project at /path/to/MyApp/MyApp.

    2. Use the pod install command.

      pod install

    Your iOS app's Debug and Release build configurations embed the corresponding Flutter components for that build mode.

  3. Build the project.

    1. Open MyApp.xcworkspace in Xcode.

      Verify that you're opening MyApp.xcworkspace and not opening MyApp.xcodeproj. The .xcworkspace file has the CocoaPod dependencies, the .xcodeproj doesn't.

    2. Select Product > Build or press Cmd + B.

Set LLDB Init File

#
  1. Generate Flutter LLDB files.

    1. Within your flutter application, run the following:
    flutter build ios --config-only

    This will generate the LLDB files in the .ios/Flutter/ephemeral directory.

  2. Set the LLDB Init File.

    1. Go to Product > Scheme > Edit Scheme.

    2. Select the Run section in the left side bar.

    3. Set the LLDB Init File using the same relative path to your Flutter application as you put in your Podfile in the Update your Podfile section.

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

      If your scheme already has an LLDB Init File, you can add Flutter's LLDB file to it. The path to Flutter's LLDB Init File must be relative to the location of your project's LLDB Init File.

      For example, if your LLDB file is located at /path/to/MyApp/.lldbinit, you would add the following:

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

Link and Embed frameworks in Xcode

#

Approach

#

In this second method, edit your existing Xcode project, generate the necessary frameworks, and embed them in your app. Flutter generates iOS frameworks for Flutter itself, for your compiled Dart code, and for each of your Flutter plugins. Embed these frameworks and update your existing application's build settings.

Requirements

#

No additional software or hardware requirements are needed for this method. Use this method in the following use cases:

  • Members of your team can't install the Flutter SDK and CocoaPods
  • You don't want to use CocoaPods as a dependency manager in existing iOS apps

Limitations

#

Flutter can't handle common dependencies with xcframeworks. If both the host app and the Flutter module's plugin define the same pod dependency and you integrate Flutter module using this option, errors result. These errors include issues like Multiple commands produce 'CommonDependency.framework'.

To work around this issue, link every plugin source in its podspec file from the Flutter module to the host app's Podfile. Link the source instead of the plugins' xcframework framework. The next section explains how to produce that framework.

To prevent the error that occurs when common dependencies exist, use flutter build ios-framework with the --no-plugins flag.

Example project structure

#

The following example assumes that you want to generate the frameworks to /path/to/MyApp/Flutter/.

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

Run this every time you change code in your Flutter module.

The resulting project structure should resemble this directory tree.

/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

Procedures

#

How you link, embed, or both the generated frameworks into your existing app in Xcode depends on the type of framework.

Flutter plugins might produce static or dynamic frameworks. Link static frameworks, never embed them.

If you embed a static framework into your iOS app, you can't publish that app to the App Store. Publishing fails with a Found an unexpected Mach-O header code archive error.

#

To link the necessary frameworks, follow this procedure.

  1. Choose the frameworks to link.

    1. In the Project Navigator, click on your project.

    2. Click the Build Phases tab.

    3. Expand Link Binary With Libraries.

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

    4. Click + (plus sign).

    5. Click Add Other... then Add Files....

    6. From the Choose frameworks and libraries to add: dialog box, navigate to the /path/to/MyApp/Flutter/Release/ directory.

    7. Command-click the frameworks in that directory then click Open.

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

  2. Update the paths to the libraries to account for build modes.

    1. Launch the Finder.

    2. Navigate to the /path/to/MyApp/ directory.

    3. Right-click on MyApp.xcodeproj and select Show Package Contents.

    4. Open project.pbxproj with Xcode. The file opens in Xcode's text editor. This also locks Project Navigator until you close the text editor.

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

    5. Find the lines that resemble the following text in the /* 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. Change the Release text highlighted in the prior step and change it to $(CONFIGURATION). Also wrap the path in quotation marks.

      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. Update the search paths.

    1. Click the Build Settings tab.

    2. Navigate to Search Paths

    3. Double-click to the right of Framework Search Paths.

    4. In the combo box, click + (plus sign).

    5. Type $(inherited). and press Enter.

    6. Click + (plus sign).

    7. Type $(PROJECT_DIR)/Flutter/$(CONFIGURATION)/ and press Enter.

      Update **Framework Search Paths** in XcodeUpdate Framework Search Paths in Xcode

After linking the frameworks, they should display in the Frameworks, Libraries, and Embedded Content section of your target's General settings.

Embed the dynamic frameworks
#

To embed your dynamic frameworks, complete the following procedure.

  1. Navigate to General > Frameworks, Libraries, and Embedded Content.

  2. Click on each of your dynamic frameworks and select Embed & Sign.

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

    Don't include any static frameworks, including FlutterPluginRegistrant.xcframework.

  3. Click the Build Phases tab.

  4. Expand Embed Frameworks. Your dynamic frameworks should display in that section.

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

  5. Build the project.

    1. Open MyApp.xcworkspace in Xcode.

      Verify that you're opening MyApp.xcworkspace and not opening MyApp.xcodeproj. The .xcworkspace file has the CocoaPod dependencies, the .xcodeproj doesn't.

    2. Select Product > Build or press Cmd + B.

Set LLDB Init File

#
  1. Generate Flutter LLDB files.

    1. Within your flutter application, re-run flutter build ios-framework if you haven't already:
    flutter build ios-framework --output=/path/to/MyApp/Flutter/

    This will generate the LLDB files in the /path/to/MyApp/Flutter/ directory.

  2. Set the LLDB Init File.

    1. Go to Product > Scheme > Edit Scheme.

    2. Select the Run section in the left side bar.

    3. Set the LLDB Init File to the following:

      $(PROJECT_DIR)/Flutter/flutter_lldbinit

      If your scheme already has an LLDB Init File, you can add Flutter's LLDB file to it. The path to Flutter's LLDB Init File must be relative to the location of your project's LLDB Init File.

      For example, if your LLDB file is located at /path/to/MyApp/.lldbinit, you would add the following:

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

Use frameworks in Xcode and Flutter framework as podspec

#

Approach

#

This method generates Flutter as a CocoaPods podspec instead of distributing the large Flutter.xcframework to other developers, machines, or continuous integration systems. Flutter still generates iOS frameworks for your compiled Dart code, and for each of your Flutter plugins. Embed these frameworks and update your existing application's build settings.

Requirements

#

No additional software or hardware requirements are needed for this method. Use this method in the following use cases:

  • Members of your team can't install the Flutter SDK and CocoaPods
  • You don't want to use CocoaPods as a dependency manager in existing iOS apps

Limitations

#

Flutter can't handle common dependencies with xcframeworks. If both the host app and the Flutter module's plugin define the same pod dependency and you integrate Flutter module using this option, errors result. These errors include issues like Multiple commands produce 'CommonDependency.framework'.

To work around this issue, link every plugin source in its podspec file from the Flutter module to the host app's Podfile. Link the source instead of the plugins' xcframework framework. The next section explains how to produce that framework.

To prevent the error that occurs when common dependencies exist, use flutter build ios-framework with the --no-plugins flag.

This method only works with the beta or stable release channels.

Example project structure

#

The following example assumes that you want to generate the frameworks to /path/to/MyApp/Flutter/.

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

Run this every time you change code in your Flutter module.

The resulting project structure should resemble this directory tree.

/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

Add Flutter engine to your Podfile

#

Host apps using CocoaPods can add the Flutter engine to their Podfile.

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

Flutter plugins might produce static or dynamic frameworks. Link static frameworks, never embed them.

If you embed a static framework into your iOS app, you can't publish that app to the App Store. Publishing fails with a Found an unexpected Mach-O header code archive error.

#

To link the necessary frameworks, follow this procedure.

  1. Choose the frameworks to link.

    1. In the Project Navigator, click on your project.

    2. Click the Build Phases tab.

    3. Expand Link Binary With Libraries.

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

    4. Click + (plus sign).

    5. Click Add Other... then Add Files....

    6. From the Choose frameworks and libraries to add: dialog box, navigate to the /path/to/MyApp/Flutter/Release/ directory.

    7. Command-click the frameworks in that directory then click Open.

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

  2. Update the paths to the libraries to account for build modes.

    1. Launch the Finder.

    2. Navigate to the /path/to/MyApp/ directory.

    3. Right-click on MyApp.xcodeproj and select Show Package Contents.

    4. Open project.pbxproj with Xcode. The file opens in Xcode's text editor. This also locks Project Navigator until you close the text editor.

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

    5. Find the lines that resemble the following text in the /* 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. Change the Release text highlighted in the prior step and change it to $(CONFIGURATION). Also wrap the path in quotation marks.

      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. Update the search paths.

    1. Click the Build Settings tab.

    2. Navigate to Search Paths

    3. Double-click to the right of Framework Search Paths.

    4. In the combo box, click + (plus sign).

    5. Type $(inherited). and press Enter.

    6. Click + (plus sign).

    7. Type $(PROJECT_DIR)/Flutter/$(CONFIGURATION)/ and press Enter.

      Update **Framework Search Paths** in XcodeUpdate Framework Search Paths in Xcode

After linking the frameworks, they should display in the Frameworks, Libraries, and Embedded Content section of your target's General settings.

Embed the dynamic frameworks
#

To embed your dynamic frameworks, complete the following procedure.

  1. Navigate to General > Frameworks, Libraries, and Embedded Content.

  2. Click on each of your dynamic frameworks and select Embed & Sign.

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

    Don't include any static frameworks, including FlutterPluginRegistrant.xcframework.

  3. Click the Build Phases tab.

  4. Expand Embed Frameworks. Your dynamic frameworks should display in that section.

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

  5. Build the project.

    1. Open MyApp.xcworkspace in Xcode.

      Verify that you're opening MyApp.xcworkspace and not opening MyApp.xcodeproj. The .xcworkspace file has the CocoaPod dependencies, the .xcodeproj doesn't.

    2. Select Product > Build or press Cmd + B.

Set LLDB Init File

#
  1. Generate Flutter LLDB files.

    1. Within your flutter application, re-run flutter build ios-framework if you haven't already:
    flutter build ios-framework --output=/path/to/MyApp/Flutter/

    This will generate the LLDB files in the /path/to/MyApp/Flutter/ directory.

  2. Set the LLDB Init File.

    1. Go to Product > Scheme > Edit Scheme.

    2. Select the Run section in the left side bar.

    3. Set the LLDB Init File to the following:

      $(PROJECT_DIR)/Flutter/flutter_lldbinit

      If your scheme already has an LLDB Init File, you can add Flutter's LLDB file to it. The path to Flutter's LLDB Init File must be relative to the location of your project's LLDB Init File.

      For example, if your LLDB file is located at /path/to/MyApp/.lldbinit, you would add the following:

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

設定本地網路隱私權限

#

在 iOS 14 及之後的版本,請在你 iOS 應用程式的 Debug 版本中啟用 Dart 的 multicast DNS 服務。 這將透過 flutter attach 增加如 hot-reload 和 DevTools 等除錯功能

若只想在 Debug 版本中設定本地網路隱私權限, 請針對每個建置組態建立獨立的 Info.plist。 SwiftUI 專案預設沒有 Info.plist 檔案。 如果你需要建立屬性清單(property list), 可以透過 Xcode 或文字編輯器完成。 以下說明假設你使用預設的 DebugRelease 組態。 若你的應用程式有不同的建置組態,請依需求調整名稱。

  1. 建立新的屬性清單(property list)。

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

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

    3. 在編輯器區的 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. 建立 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. Bonjour Services 陣列中新增字串值 _dartVmService._tcp

    3. (選用) 若要自訂權限對話框文字, 請新增鍵值 Privacy - Local Network Usage Description

      已加入 **Bonjour Services** 及 **Privacy - Local Network Usage Description** 鍵值的 `Info-Debug` 屬性清單已加入 Bonjour ServicesPrivacy - Local Network Usage Description 鍵值的 Info-Debug 屬性清單

  5. 設定 target 以便不同建置模式使用不同屬性清單。

    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

      將 `Info.plist` build setting 更新為依建置模式使用專屬屬性清單Info.plist build setting 更新為依建置模式使用專屬屬性清單

      這樣在 Debug 模式下會使用 Info-Debug.plist, 在 Release 模式下會使用 Info-Release.plist

      更新後的 **Info.plist File** build setting,顯示不同組態的設定更新後的 Info.plist File build setting,顯示不同組態的設定

  6. Build Phases 中移除 Release 屬性清單。

    1. Project Navigator 中點選你的專案。

    2. 點選 Build Phases 分頁。

    3. 展開 Copy Bundle Resources

    4. 如果清單中包含 Info-Release.plist, 請點選它,然後點選下方的 -(減號) 以將該屬性清單從資源清單中移除。

      **Copy Bundle** build phase 顯示 **Info-Release.plist** 設定。請移除此設定。Copy Bundle build phase 顯示 Info-Release.plist 設定。請移除此設定。

  7. 你的 Debug 應用程式載入的第一個 Flutter 螢幕會提示 取得本地網路權限。

    請點選 OK

    (選用) 若要在應用程式載入前即授權,請到 設定 > 隱私權 > 本地網路 > 你的應用程式 中啟用。

解決 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

    將 `arm64` 加入為應用程式的排除架構arm64 加入為應用程式的排除架構

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

  13. 針對 Release 建置模式重複上述步驟。

  14. 對所有 iOS 單元測試目標也重複上述步驟。

下一步

#

你現在可以將 Flutter 螢幕 加入到你現有的 iOS 應用程式中。