給應用程式開發者的 Swift Package Manager
Flutter 整合 Swift Package Manager 有以下幾項優點:
- 可存取 Swift 套件生態系。 Flutter 套件(plugins)可以利用日益壯大的 Swift 套件 生態系。
- 簡化 Flutter 安裝流程。 Xcode 已內建 Swift Package Manager。 如果你的專案使用 Swift Package Manager,就不需要安裝 Ruby 與 CocoaPods。
How to turn on Swift Package Manager
#Flutter's Swift Package Manager support is turned off by default. To turn it on:
Upgrade to the latest Flutter SDK:
shflutter upgradeTurn on the Swift Package Manager feature:
shflutter config --enable-swift-package-manager
Using the Flutter CLI to run an app migrates the project to add Swift Package Manager integration. This makes your project download the Swift packages that your Flutter plugins depend on. An app with Swift Package Manager integration requires Flutter version 3.24 or higher. To use an older Flutter version, you will need to remove Swift Package Manager integration from the app.
Flutter falls back to CocoaPods for dependencies that do not support Swift Package Manager yet.
How to turn off Swift Package Manager
#Disabling Swift Package Manager causes Flutter to use CocoaPods for all dependencies. However, Swift Package Manager remains integrated with your project. To remove Swift Package Manager integration completely from your project, follow the How to remove Swift Package Manager integration instructions.
Turn off for a single project
#In the project's pubspec.yaml file, under the flutter section, add disable-swift-package-manager: true.
# The following section is specific to Flutter packages.
flutter:
disable-swift-package-manager: trueThis turns off Swift Package Manager for all contributors to this project.
Turn off globally for all projects
#Run the following command:
flutter config --no-enable-swift-package-managerThis turns off Swift Package Manager for the current user.
If a project is incompatible with Swift Package Manager, all contributors need to run this command.
如何新增 Swift Package Manager 整合
#新增至 Flutter 應用程式
#Once you turn on Swift Package Manager, the Flutter CLI tries to migrate your project the next time you run your app using the CLI. This migration updates your Xcode project to use Swift Package Manager to add Flutter plugin dependencies.
To migrate your project:
Run the iOS app using the Flutter CLI.
If your iOS project doesn't have Swift Package Manager integration yet, the Flutter CLI tries to migrate your project and outputs something like:
flutter run Adding Swift Package Manager integration...The automatic iOS migration modifies the
ios/Runner.xcodeproj/project.pbxprojandios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcschemefiles.If the Flutter CLI's automatic migration fails, follow the steps in add Swift Package Manager integration manually.
[Optional] To check if your project is migrated:
Run the app in Xcode.
Ensure that Run Prepare Flutter Framework Script runs as a pre-action and that
FlutterGeneratedPluginSwiftPackageis a target dependency.
Ensure Run Prepare Flutter Framework Script runs as a pre-action
Once you turn on Swift Package Manager, the Flutter CLI tries to migrate your project the next time you run your app using the CLI. This migration updates your Xcode project to use Swift Package Manager to add Flutter plugin dependencies.
To migrate your project:
Run the macOS app using the Flutter CLI.
If your macOS project doesn't have Swift Package Manager integration yet, the Flutter CLI tries to migrate your project and outputs something like:
flutter run -d macos Adding Swift Package Manager integration...The automatic iOS migration modifies the
macos/Runner.xcodeproj/project.pbxprojandmacos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcschemefiles.If the Flutter CLI's automatic migration fails, follow the steps in add Swift Package Manager integration manually.
[Optional] To check if your project is migrated:
Run the app in Xcode.
Ensure that Run Prepare Flutter Framework Script runs as a pre-action and that
FlutterGeneratedPluginSwiftPackageis a target dependency.
Ensure Run Prepare Flutter Framework Script runs as a pre-action
手動 新增至 Flutter 應用程式
#Once you turn on Swift Package Manager, the Flutter CLI tries to migrate your project to use Swift Package Manager the next time you run your app using the CLI.
However, the Flutter CLI tool might be unable to migrate your project automatically if there are unexpected modifications.
If the automatic migration fails, use the steps below to add Swift Package Manager integration to a project manually.
Before migrating manually, file an issue; this helps the Flutter team improve the automatic migration process. Include the error message and, if possible, include a copy of the following files in your issue:
ios/Runner.xcodeproj/project.pbxprojios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme(or the xcsheme for the flavor used)
Step 1: Add FlutterGeneratedPluginSwiftPackage Package Dependency
#Open your app (
ios/Runner.xcworkspace) in Xcode.Navigate to Package Dependencies for the project.

The project's package dependencies Click add.
In the dialog that opens, click Add Local....
Navigate to
ios/Flutter/ephemeral/Packages/FlutterGeneratedPluginSwiftPackageand click Add Package.Ensure that it's added to the
Runnertarget and click Add Package.
Ensure that the package is added to the RunnertargetEnsure that
FlutterGeneratedPluginSwiftPackagewas added to Frameworks, Libraries, and Embedded Content.
Ensure that FlutterGeneratedPluginSwiftPackagewas added to Frameworks, Libraries, and Embedded Content
Step 2: Add Run Prepare Flutter Framework Script Pre-Action
#The following steps must be completed for each flavor.
Go to Product > Scheme > Edit Scheme.
Expand the Build section in the left side bar.
Click Pre-actions.
Click add and select New Run Script Action from the menu.
Click the Run Script title and change it to:
Run Prepare Flutter Framework ScriptChange the Provide build settings from to the
Runnerapp.Input the following in the text box:
sh"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" prepare
Add Run Prepare Flutter Framework Script build pre-action
Step 3: Run app
#Run the app in Xcode.
Ensure that Run Prepare Flutter Framework Script runs as a pre-action and that
FlutterGeneratedPluginSwiftPackageis a target dependency.
Ensure Run Prepare Flutter Framework Script runs as a pre-action Ensure that the app runs on the command line with
flutter run.
Once you turn on Swift Package Manager, the Flutter CLI tries to migrate your project to use Swift Package Manager the next time you run your app using the CLI.
However, the Flutter CLI tool might be unable to migrate your project automatically if there are unexpected modifications.
If the automatic migration fails, use the steps below to add Swift Package Manager integration to a project manually.
Before migrating manually, file an issue; this helps the Flutter team improve the automatic migration process. Include the error message and, if possible, include a copy of the following files in your issue:
macos/Runner.xcodeproj/project.pbxprojmacos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme(or the xcscheme for the flavor used)
Step 1: Add FlutterGeneratedPluginSwiftPackage Package Dependency
#Open your app (
macos/Runner.xcworkspace) in Xcode.Navigate to Package Dependencies for the project.

The project's package dependencies Click add.
In the dialog that opens, click the Add Local....
Navigate to
macos/Flutter/ephemeral/Packages/FlutterGeneratedPluginSwiftPackageand click the Add Package.Ensure that it's added to the Runner Target and click Add Package.

Ensure that the package is added to the RunnertargetEnsure that
FlutterGeneratedPluginSwiftPackagewas added to Frameworks, Libraries, and Embedded Content.
Ensure that FlutterGeneratedPluginSwiftPackagewas added to Frameworks, Libraries, and Embedded Content
Step 2: Add Run Prepare Flutter Framework Script Pre-Action
#The following steps must be completed for each flavor.
Go to Product > Scheme > Edit Scheme.
Expand the Build section in the left side bar.
Click Pre-actions.
Click the add button and select New Run Script Action from the menu.
Click the Run Script title and change it to:
Run Prepare Flutter Framework ScriptChange the Provide build settings from to the
Runnertarget.Input the following in the text box:
sh"$FLUTTER_ROOT"/packages/flutter_tools/bin/macos_assemble.sh prepare
Add Run Prepare Flutter Framework Script build pre-action
Step 3: Run app
#Run the app in Xcode.
Ensure that Run Prepare Flutter Framework Script runs as a pre-action and that
FlutterGeneratedPluginSwiftPackageis a target dependency.
Ensure Run Prepare Flutter Framework Scriptruns as a pre-actionEnsure that the app runs on the command line with
flutter run.
新增至現有應用程式(add-to-app)
#Flutter 的 Swift Package Manager 支援目前不適用於 add-to-app 情境。
若想掌握最新狀態,請參考 flutter#146957。
新增至自訂 Xcode target
#你的 Flutter Xcode 專案可以擁有自訂的 Xcode targets,用來建置額外產品,例如 framework 或單元測試。 你可以將 Swift Package Manager 整合新增至這些自訂 Xcode target。
請依照 如何手動將 Swift Package Manager 整合新增至專案 的步驟操作。
在 步驟 1,第 6 項,請使用你的自訂 target 取代 Flutter target。
在 步驟 2,第 6 項,請使用你的自訂 target 取代 Flutter target。
如何移除 Swift Package Manager 整合
#為了新增 Swift Package Manager 整合,Flutter CLI 會遷移你的專案。 這個遷移會更新你的 Xcode 專案,以加入 Flutter 套件(plugin)相依性。
若要還原這個遷移:
清理你的專案:
shflutter clean在 Xcode 中開啟你的應用程式(
ios/Runner.xcworkspace或macos/Runner.xcworkspace)。前往專案的 Package Dependencies(套件相依性)。
點選
FlutterGeneratedPluginSwiftPackage套件,然後點擊 remove。
要移除的 FlutterGeneratedPluginSwiftPackage前往
Runnertarget 的 Frameworks, Libraries, and Embedded Content(框架、程式庫與嵌入內容)。點選
FlutterGeneratedPluginSwiftPackage,然後點擊 remove。
要移除的 FlutterGeneratedPluginSwiftPackage前往 Product > Scheme > Edit Scheme。
在左側邊欄展開 Build 區段。
點選 Pre-actions(預先動作)。
展開 Run Prepare Flutter Framework Script。
點選 delete。

要移除的建置前動作
如何使用需要較高作業系統版本的 Swift Package Manager Flutter 插件
#如果某個 Swift Package Manager Flutter 插件所需的作業系統版本高於你的專案設定,可能會出現如下錯誤:
Target Integrity (Xcode): The package product 'plugin_name_ios' requires minimum platform version 14.0 for the iOS platform, but this target supports 12.0要使用此插件:
在 Xcode 中開啟您的應用程式(
ios/Runner.xcworkspace或macos/Runner.xcworkspace)。提高您的應用程式目標的Minimum Deployments(最低部署版本)設定。

目標的 Minimum Deployments 設定 如果您已更新 iOS 應用程式的 Minimum Deployments,請重新產生 iOS 專案的組態檔案:
shflutter build ios --config-only如果你已更新 macOS 應用程式的最低部署版本(Minimum Deployments),
請重新產生 macOS 專案的設定檔案:shflutter build macos --config-only