将 Flutter 应用集成到 iOS 项目
了解如何将 Flutter 应用集成到你现有的 iOS 项目中。
Flutter UI components can be incrementally added into your existing iOS application using Swift packages.
Prerequisites
#- Flutter 3.44 or later
- Xcode 15.0 or later
Migrate from Legacy Integration (If Applicable)
#If you've already integrated Flutter into your iOS app using CocoaPods or embedded frameworks, you must first remove that integration before following the Swift Package Manager instructions below.
Expand to see instructions to migrate from CocoaPods integration
If your app was previously integrated using CocoaPods, you must first remove the Flutter installation code from your Podfile.
Remove Flutter installation code from your Podfile
MyApp/Podfilerubyflutter_application_path = '../my_flutter' load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb') install_all_flutter_pods(flutter_application_path) flutter_post_install(installer) if defined?(flutter_post_install)Run
pod install.
Expand to see instructions to migrate from embedded frameworks integration
If your app was previously integrated using frameworks
generated by the flutter build ios-framework command,
you must first remove the frameworks from your Xcode project.
Navigate to your target's General tab and remove all Flutter-related frameworks and libraries under Frameworks, Libraries, and Embedded Content.
This includes the
App.xcframework,Flutter.xcframework,FlutterPluginRegistrant.xcframework, and any Flutter plugins'xcframeworkfiles.Remove the Flutter pod from your Podfile
MyApp/Podfilerubypod 'Flutter', :podspec => '/path/to/MyApp/Flutter/[build mode]/Flutter.podspec'Run
pod install.
The legacy integration guide is preserved for reference, but will not receive ongoing maintenance.
Organize your projects relative to each other
#This guide assumes that your existing iOS app and your Flutter app or module reside in sibling directories. If you have a different directory structure, you will need to adjust the example relative paths accordingly.
The example directory structure resembles the following:
-
my_flutter_app/
- ios/
lib/
- main.dart
-
MyNativeApp/
- MyNativeApp.xcodeproj/
-
my_flutter_app/
- .ios/
lib/
- main.dart
-
MyNativeApp/
- MyNativeApp.xcodeproj/
Integrate with Swift Package Manager
#-
Build the FlutterNativeIntegration Swift package
Within your Flutter application or module, run the following command:
flutter build swift-package --platform iosThis will generate the following directories:
my_flutter_app/build/ios/SwiftPackages/
- FlutterNativeIntegration/(A Swift package)
- Scripts/(Directory of scripts and other files needed)
You can optionally change the location of this output with the
--outputflag. -
Add FlutterNativeIntegration to your Xcode project
Open your existing iOS app in Xcode.
In the Project navigator, right click on your project and select Add Files to "MyNativeApp"...
Navigate to and select the generated
FlutterNativeIntegrationSwift package and click Add.Select Reference files in place and click Finish.
In the File inspector, verify the Location is Relative to Project. If it is not, you'll need to move the Flutter output directory to be a sibling directory of your native app.

Relative location of FlutterNativeIntegration shown in Xcode's File inspector.
Navigate to your target's General tab and add
FlutterNativeIntegrationunder Frameworks, Libraries, and Embedded Content.
FlutterNativeIntegration under Frameworks, Libraries, and Embedded Content.
-
Add build settings
In the Build Settings tab, set the location of the Flutter app's Swift package output directory:
FLUTTER_SWIFT_PACKAGE_OUTPUT=$SRCROOT/../my_flutter_app/build/ios/SwiftPackagesFor custom configurations, set the Flutter build mode.
Flutter supports three build modes: Debug, Profile, and Release. The build mode is determined using the
CONFIGURATION. If your configuration does not match one of these, you can set theFLUTTER_BUILD_MODEbuild setting to one of these values.
Setting
FLUTTER_BUILD_MODEfor custom configurations under Build Settings.(Optional) Allow Xcode to re-build your Flutter app.
Add the below build settings to your target to allow Xcode to re-build your Flutter app as part of its build. This allows you to make changes to your Flutter application without needing to re-run
flutter build swift-package. This requires Flutter to be installed on the machine.FLUTTER_APPLICATION_PATH=$SRCROOT/../my_flutter_app ENABLE_USER_SCRIPT_SANDBOXING=NO
-
Add Pre-action Run Script to Scheme
Open Product > Scheme > Edit Scheme... > Build (in left side bar) > Pre-action > + > New Run Script Action
Select your project in the Provide build settings from dropdown.
Set the script to the following:
/bin/sh $FLUTTER_SWIFT_PACKAGE_OUTPUT/Scripts/flutter_integration.sh prebuild

Pre-action Run Script in scheme editor.
-
Add New Run Script Build Phase to Target
Navigate to your target's Build Phases > + > New Run Script Phase
Set the script to the following:
/bin/sh $FLUTTER_SWIFT_PACKAGE_OUTPUT/Scripts/flutter_integration.sh assembleUncheck Based on dependency analysis
Add the following to Input File Lists:
$(FLUTTER_SWIFT_PACKAGE_OUTPUT)/Scripts/FlutterAssembleInputs.xcfilelist

New Run Script Build Phase under Build Phases.
-
(Optional) Set LLDB Init File
Using Flutter's LLDB Init File improves performance when debugging on physical iOS 26+ devices.
Open Product > Scheme > Edit Scheme... > Run (in left side bar).
Set the LLDB Init File to the following path:
$(FLUTTER_SWIFT_PACKAGE_OUTPUT)/Scripts/flutter_lldbinitAlternatively, 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.
command source --relative-to-command-file "../my_flutter_app/build/ios/SwiftPackages/Scripts/flutter_lldbinit"
Set local network privacy permissions
#
在 iOS 14 及更高版本上,请在 iOS app 的 Debug 版本中启用 Dart 多播 DNS 服务。这样可通过 flutter attach 使用热重载和 DevTools 等调试功能。
若仅在 app 的 Debug 版本中设置本地网络隐私权限,请为每个构建配置创建单独的 Info.plist。
SwiftUI 项目一开始可能没有 Info.plist 文件。如需创建属性列表,可通过 Xcode 或文本编辑器完成。以下说明假定使用默认的 Debug 和 Release。请根据 app 的构建配置按需调整名称。
-
Create a new property list.
创建新的属性列表。
Open your project in Xcode.
- 在 Xcode 中打开项目。
In the Project Navigator, click on the project name.
- 在 Project Navigator 中点击项目名称。
From the Targets list in the Editor pane, click on your app.
- 在编辑器窗格的 Targets 列表中点击你的 app。
Click the Info tab.
- 点击 Info 标签页。
Expand Custom iOS Target Properties.
- 展开 Custom iOS Target Properties。
Right-click on the list and select Add Row.
- 右键点击列表,选择 Add Row。
From the dropdown menu, select Bonjour Services. This creates a new property list in the project directory called
Info. This displays asInfo.plistin the Finder.- 在下拉菜单中选择 Bonjour Services。这会在项目目录中创建一个名为
Info的新属性列表。在 Finder 中显示为Info.plist。
- 在下拉菜单中选择 Bonjour Services。这会在项目目录中创建一个名为
-
Rename the
Info.plisttoInfo-Debug.plist将
Info.plist重命名为Info-Debug.plistClick on Info file in the project list at the left.
- 在左侧项目列表中点击 Info 文件。
In the Identity and Type panel at the right, change the Name from
Info.plisttoInfo-Debug.plist.- 在右侧 Identity and Type 面板中,将 Name 从
Info.plist改为Info-Debug.plist。
- 在右侧 Identity and Type 面板中,将 Name 从
-
Create a Release property list.
创建 Release 属性列表。
In the Project Navigator, click on
Info-Debug.plist.- 在 Project Navigator 中点击
Info-Debug.plist。
- 在 Project Navigator 中点击
Select File > Duplicate.... You can also press Cmd + Shift + S.
- 选择 File > Duplicate...。也可按 Cmd + Shift + S。
In the dialog box, set the Save As: field to
Info-Release.plistand click Save.- 在对话框中将 Save As: 设为
Info-Release.plist,然后点击 Save。
- 在对话框中将 Save As: 设为
-
Add the necessary properties to the Debug property list.
向 Debug 属性列表添加必要属性。
In the Project Navigator, click on
Info-Debug.plist.- 在 Project Navigator 中点击
Info-Debug.plist。
- 在 Project Navigator 中点击
Add the String value
_dartVmService._tcpto the Bonjour Services array.- 向 Bonjour Services 数组添加字符串值
_dartVmService._tcp。
- 向 Bonjour Services 数组添加字符串值
(Optional) To set your desired customized permission dialog text, add the key Privacy - Local Network Usage Description.
- (可选) 若要设置自定义权限对话框文案,请添加键 Privacy - Local Network Usage Description。

The
Info-Debugproperty list with the Bonjour Services and Privacy - Local Network Usage Description keys added
已添加 Bonjour Services 与 Privacy - Local Network Usage Description 键的
Info-Debug属性列表
-
Set the target to use different property lists for different build modes.
设置 target 在不同构建模式下使用不同属性列表。
In the Project Navigator, click on your project.
- 在 Project Navigator 中点击你的项目。
Click the Build Settings tab.
- 点击 Build Settings 标签页。
Click All and Combined sub-tabs.
- 点击 All 和 Combined 子标签页。
In the Search box, type
plist. This limits the settings to those that include property lists.- 在搜索框中输入
plist,将设置限定为与属性列表相关的项。
- 在搜索框中输入
Scroll through the list until you see Packaging.
- 滚动列表直至看到 Packaging。
Click on the Info.plist File setting.
- 点击 Info.plist File 设置。
Change the Info.plist File value from
path/to/Info.plisttopath/to/Info-$(CONFIGURATION).plist.- 将 Info.plist File 的值从
path/to/Info.plist改为path/to/Info-$(CONFIGURATION).plist。

Updating the
Info.plistbuild 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 构建设置,显示各配置差异
- 将 Info.plist File 的值从
-
Remove the Release property list from the Build Phases.
从 Build Phases 中移除 Release 属性列表。
In the Project Navigator, click on your project.
- 在 Project Navigator 中点击你的项目。
Click the Build Phases tab.
- 点击 Build Phases 标签页。
Expand Copy Bundle Resources.
- 展开 Copy Bundle Resources。
If this list includes
Info-Release.plist, click on it and then click the - (minus sign) under it to remove the property list from the resources list.- 若列表包含
Info-Release.plist,请点击它,再点击下方 -(减号)将其从资源列表中移除。

The Copy Bundle build phase displaying the Info-Release.plist setting. Remove this setting.

Copy Bundle 构建阶段显示 Info-Release.plist 设置。请移除此设置。
- 若列表包含
-
The first Flutter screen your Debug app loads prompts for local network permission.
- Debug app 加载的第一个 Flutter 界面会提示本地网络权限。
点击 OK。
(可选) 若要在 app 加载前授予权限,请启用 Settings > Privacy > Local Network > Your App。
Next steps
#You can now add a Flutter screen to your existing iOS app.
除非另有说明,本文档之所提及适用于 Flutter 3.44.0 版本。本页面最后更新时间:2026-06-03。查看文档源码 或者 为本页面内容提出建议。