iOS/Flutter混合开发之PlatformView配置与使用

环境:Xcode 16.3、Flutter 3.29.2、Dart 3.7.2。

使用背景:需要在flutter界面中嵌入一个iOS原生控件。

步骤:

1. iOS侧实现

1.1:PlatformView实现

Swift 复制代码
class FLNativeView: NSObject, FlutterPlatformView {
    private var _view: UIView

    // flutter侧传入参数
    private var arguments: Any?

    init(
        frame: CGRect,
        viewIdentifier viewId: Int64,
        arguments args: Any?,
        binaryMessenger messenger: FlutterBinaryMessenger?
    ) {
        _view = UIView()
        super.init()
        _view.backgroundColor = UIColor.white
        arguments = args
        createNativeView(view: _view)
    }

    func view() -> UIView {
        _view
    }

    func createNativeView(view _view: UIView) {
        let testView = UIView() // 创建需要嵌入的视图
        constrain(testView) { testView in
            testView.edges == testView.superview!.edges
        }
    }
}

1.2:FlutterPlatformViewFactory实现

Swift 复制代码
public class FLNativeViewFactory: NSObject, FlutterPlatformViewFactory {
    private var messenger: FlutterBinaryMessenger

    public init(messenger: FlutterBinaryMessenger) {
        self.messenger = messenger
        super.init()
    }

    public func create(
        withFrame frame: CGRect,
        viewIdentifier viewId: Int64,
        arguments args: Any?
    ) -> FlutterPlatformView {
        FLNativeView(
            frame: frame,
            viewIdentifier: viewId,
            arguments: args,
            binaryMessenger: messenger
        )
    }

    public func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
        FlutterStandardMessageCodec.sharedInstance()  // 用于参数编解码
    }
}

1.3:FlutterPlugin实现

Swift 复制代码
public class NativeWebViewPlugin: NSObject, FlutterPlugin {
    static let viewId = "platform-view-test"

    public static func register(with registrar: FlutterPluginRegistrar) {
        let factory = FLNativeViewFactory(messenger: registrar.messenger())
        registrar.register(factory, withId: NativeWebViewPlugin.viewId) // 唯一标识符
    }
}

1.4:注册FlutterPlugin(可使用引擎注册、或在FlutterViewController中注册,具体方式结合flutter集成方式和使用场景,这里采用后者)

在目标flutter控制器(需要嵌入原生组件、且继承自FlutterViewController的控制器)中执行注册方法:

Swift 复制代码
    func registrarPlugin() {
        if !hasPlugin("NativeWebViewPlugin") {
            let registry = registrar(forPlugin: "NativeWebViewPlugin")
            registry?.register(
                FLNativeViewFactory(messenger: binaryMessenger),
                withId: NativeWebViewPlugin.viewId
            )
        }
    }

2. flutter侧实现

Dart 复制代码
UiKitView(
   viewType: "platform-view-test",
   layoutDirection: TextDirection.ltr,
   creationParams: {'key': 'test'},
   creationParamsCodec: const StandardMessageCodec(),
);
相关推荐
苦逼的搬砖工8 分钟前
Flutter UI Components:闲来无事,设计整理了这几年来使用的UI组件库
前端·flutter
2501_915918411 小时前
uni-app 项目 iOS 上架效率优化 从工具选择到流程改进的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张2 小时前
如何在不同 iOS 设备上测试和上架 uni-app 应用 实战全流程解析
android·ios·小程序·https·uni-app·iphone·webview
黑金IT2 小时前
Dart → `.exe`:Flutter 桌面与纯命令行双轨编译完全指南
flutter
iOS_MingXing2 小时前
flutter TabBar 设置isScrollable 第一个有间距
flutter
wjm0410062 小时前
ios面试八股文
ios·面试
张较瘦_5 小时前
[论文阅读] 人工智能 + 软件工程 | 大模型破局跨平台测试!LLMRR让iOS/安卓/鸿蒙脚本无缝迁移
论文阅读·人工智能·ios
m0_6410310514 小时前
在选择iOS代签服务前,你必须了解的三大安全风险
ios
开开心心loky16 小时前
[iOS] push 和 present Controller 的区别
ui·ios·objective-c·cocoa
大熊猫侯佩16 小时前
10 个 Xcode 神技:哥谭开发者必学的 IDE 对抗术
xcode·swift·apple