Sub topic 5
Integrating a Flutter Module into an iOS App
This chapter assumes basic Dart and Flutter knowledge, plus familiarity with Swift, Xcode, CocoaPods, and view controllers. You will connect a Flutter module to an existing iOS app and present a Flutter screen from native Swift code.
How the iOS integration works
An iOS host app does not embed Dart source directly. CocoaPods connects the host project to the Flutter module's generated iOS project. At runtime, a FlutterEngine runs Dart code, and a FlutterViewController displays that engine's Flutter UI.
| Part | Responsibility |
|---|---|
| CocoaPods | Adds the Flutter module and its plugins to the iOS build |
| `FlutterEngine` | Runs Dart code and manages Flutter execution |
| `FlutterViewController` | Displays the Flutter UI inside the native app |
The module contains a hidden .ios directory. Flutter generates this directory for iOS integration. Do not edit generated files inside it.
Add the module with CocoaPods
Create the module next to the existing iOS project:
flutter create --template module my_flutter_moduleAssume this directory layout:
workspace/
NativeApp/
NativeApp.xcodeproj
Podfile
my_flutter_module/
pubspec.yaml
lib/Open the native app's Podfile and add the Flutter module path and helper calls:
flutter_application_path = '../my_flutter_module'
load File.join(
flutter_application_path,
'.ios',
'Flutter',
'podhelper.rb'
)
target 'NativeApp' do
install_all_flutter_pods(flutter_application_path)
end
post_install do |installer|
flutter_post_install(installer)
endKeep your existing pods and target settings. Add the Flutter lines inside the matching application target. Then install the pods:
cd NativeApp
flutter pub get
pod install
open NativeApp.xcworkspaceUse the .xcworkspace file after running pod install. Opening the original .xcodeproj can make Xcode unable to find Flutter or plugin frameworks.
Create and show a Flutter engine
The host app needs a FlutterEngine. A good starting design creates one dependency object and keeps the engine available for screens that need Flutter.
Create a Swift file in the native app:
import UIKit
import Flutter
import FlutterPluginRegistrant
final class FlutterDependencies {
let engine = FlutterEngine(name: "main_engine")
init() {
engine.run()
GeneratedPluginRegistrant.register(with: engine)
}
}
final class HomeViewController: UIViewController {
var dependencies: FlutterDependencies!
@IBAction func showFlutter(_ sender: UIButton) {
let flutterViewController = FlutterViewController(
engine: dependencies.engine,
nibName: nil,
bundle: nil
)
present(flutterViewController, animated: true)
}
}Initialize dependencies before HomeViewController receives the button action. For example, an application coordinator or the app's initial view-controller setup can create FlutterDependencies and assign the same instance to the home screen.
engine.run() starts the default Dart entrypoint, which is the module's main() function. GeneratedPluginRegistrant.register(with:) makes Flutter plugins available to that engine. The FlutterViewController then attaches to the running engine.
Understand the object relationship
The engine and view have different lifetimes. A view controller can be presented and dismissed while the engine continues running. This lets a later Flutter screen reuse the engine, although the engine's memory remains allocated.
Common integration errors
- Opening the
.xcodeprojinstead of the.xcworkspace. - Using a module path that is relative to the wrong
Podfile. - Forgetting
GeneratedPluginRegistrant.register(with:), which prevents plugin registration. - Creating a new engine every time without considering startup time and memory use.
- Presenting a
FlutterViewControllerbefore its engine has run. - Testing only on the simulator when a plugin requires physical-device APIs.
The CocoaPods integration is a build-time connection. The engine and view are runtime objects. Keeping those responsibilities separate makes it easier to diagnose whether a failure comes from dependency installation, engine startup, or screen presentation.
Exercise
Your Flutter screen opens, but a plugin reports that it is unavailable. Which integration step should you check first?
Show answer
Check that GeneratedPluginRegistrant.register(with: engine) runs for the same FlutterEngine attached to the FlutterViewController. Also confirm that pod install was run and that the app was opened from its .xcworkspace.