Sub topic 4
Building and Registering the iOS View
This chapter assumes that you know basic Flutter and Dart, can read simple Swift, and understand the basic idea of a MethodChannel. You should also be able to run a Flutter app on an iOS simulator or device.
A platform view lets Flutter display a native iOS UIView inside its widget tree. On iOS, Flutter uses two Swift types:
- A
FlutterPlatformViewowns or wraps the nativeUIView. - A
FlutterPlatformViewFactorycreates one platform-view object for each Flutter view instance.
The factory receives the view's frame, a numeric view identifier, and optional creation arguments. Flutter then displays the UIView returned by the platform-view object.
How the iOS pieces connect
The Dart widget and the Swift factory must use exactly the same view type string. This string is an identifier, not a class name.
| Piece | Responsibility |
|---|---|
| `UiKitView` | Requests a native view from Flutter |
| View type string | Connects the Dart request to the registered factory |
| `FlutterPlatformViewFactory` | Creates each native view instance |
| `FlutterPlatformView` | Returns the actual `UIView` |
| Plugin registrar | Registers the factory with the Flutter engine |
The flow looks like this:
Implement the platform view
The following example wraps a UILabel. The constructor reads the initial text from the arguments passed by Dart.
import Flutter
import UIKit
final class NativeLabel: NSObject, FlutterPlatformView {
private let label: UILabel
init(frame: CGRect, arguments args: Any?) {
label = UILabel(frame: frame)
if let params = args as? [String: Any],
let text = params["text"] as? String {
label.text = text
} else {
label.text = ""
}
label.font = UIFont.systemFont(ofSize: 20)
label.numberOfLines = 0
label.textAlignment = .center
label.backgroundColor = .secondarySystemBackground
super.init()
}
func view() -> UIView {
return label
}
}FlutterPlatformView requires the view() method. Flutter calls this method when it needs the native view to insert into the screen.
The frame parameter contains the initial size and position requested by Flutter. The native view can use that frame when it creates its UIKit object. Flutter remains responsible for placing the platform view in the widget tree.
Implement the factory
The factory creates a new NativeLabel for every UiKitView instance.
final class NativeLabelFactory: NSObject, FlutterPlatformViewFactory {
func create(
withFrame frame: CGRect,
viewIdentifier viewId: Int64,
arguments args: Any?
) -> FlutterPlatformView {
return NativeLabel(frame: frame, arguments: args)
}
func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
return FlutterStandardMessageCodec.sharedInstance()
}
}FlutterStandardMessageCodec supports common values such as strings, numbers, lists, and dictionaries. It must match the codec used by the Dart widget. A mismatch can prevent the factory from decoding creation parameters correctly.
Register the factory
Register the factory with the iOS Flutter plugin registrar. In a standard Flutter application, this code belongs in AppDelegate.swift, after generated plugins are registered.
import Flutter
import FlutterPluginRegistrant
import UIKit
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
guard let registrar = self.registrar(forPlugin: "NativeLabelPlugin") else {
return false
}
registrar.register(
NativeLabelFactory(),
withId: "com.example.app/native_label"
)
return super.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
}
}The withId value must be identical to the viewType passed to UiKitView in Dart. Registration does not create a label immediately. It makes the factory available when Flutter later requests that view type.
Creation arguments
The Dart side can send initial data like this:
UiKitView(
viewType: 'com.example.app/native_label',
creationParams: <String, dynamic>{'text': 'Hello from Flutter'},
creationParamsCodec: const StandardMessageCodec(),
)Flutter serializes the map, passes it to create, and the Swift initializer reads the "text" value. Creation parameters are useful for configuration that is known when the view starts. For changes after creation, use a per-view method channel or another communication mechanism.
Exercise
What two values must match for Flutter to find the iOS factory, and which Swift method returns the native UIKit view?
Show answer
The Dart viewType and the registrar's withId value must match exactly. The view() method on FlutterPlatformView returns the UIKit view.