I'm using Platform View to integrate native views in my Flutter app. I successfully integrated Android view but I'm facing issue in iOS view. I followed EXACTLY the instruction in Flutter docs: Hosting native iOS views in your Flutter app with Platform Views, but my arguments somehow cannot pass to Swift native.
Dart code:
UiKitView(
viewType: '<platform-view-type>',
layoutDirection: ui_import.TextDirection.ltr,
creationParams: {"num": 1, "text": "example"}, // my argument
creationParamsCodec: StandardMessageCodec(),
)
Swift code, FLNativeView.swift
:
class FLNativeViewFactory: NSObject, FlutterPlatformViewFactory {
private var messenger: FlutterBinaryMessenger
init(messenger: FlutterBinaryMessenger) {
self.messenger = messenger
super.init()
}
func create(
withFrame frame: CGRect,
viewIdentifier viewId: Int64,
arguments args: Any?
) -> FlutterPlatformView {
return FLNativeView( // <---- breakpoint here, but args parameter is nil
frame: frame,
viewIdentifier: viewId,
arguments: args,
binaryMessenger: messenger)
}
}
My AppDelegate.swift
:
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
weak var registrar = self.registrar(forPlugin: "plugin-name")
let factory = FLNativeViewFactory(messenger: registrar!.messenger())
self.registrar(forPlugin: "<plugin-name>")!.register(
factory,
withId: "<platform-view-type>")
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
My flutter doctor
:
[✓] Flutter (Channel stable, 3.7.8, on macOS 13.4 22F66 darwin-arm64, locale en-VN)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.1)
[✓] VS Code (version 1.78.2)
[✓] Connected device (3 available)
[✓] HTTP Host Availability
P/s: The reason why I have to working on native views is that PDF functionality of Flutter is not as "full" as Kotlin/Swift.
I followed EXACTLY the instruction in Flutter docs and also look through other Flutter posts, Android works but iOS doesn't.
Oh damn I figure it out! The problem is lack of documentation. The issue has raised in GitHub: https://github.com/flutter/flutter/issues/28124. In my
FLNativeViewFactory
, add this function:Then it WORKS!