Flutter视图中嵌入iOS控件

151 阅读1分钟

前言

想要在flutter中展示iOS的视图很简单

使用UiKitView

  • viewType:通信绑定的名字要和iOS中保持一致
  • onPlatformViewCreated:回调的是创建iOS视图的动态id
  • creationParams:创建视图时需要传递的参数
  • creationParamsCodec:将 creationParams 编码后再发送给平台侧

视图绑定

在iOS中需要将视图进行绑定

XJPLPlayerFactory *factory = [[XJPLPlayerFactory alloc]initWithMessager:registrar.messenger];
[registrar registerViewFactory:factory withId:@"plugins.flutter.io/xj_pl_player_view"];

XJPLPlayerFactory.h

//
//  XJPLPlayerFactory.h
//  plugin_plplayer
//
//  Created by xj_mac on 2020/12/24.
//

#import <Foundation/Foundation.h>
#import <Flutter/Flutter.h>
NS_ASSUME_NONNULL_BEGIN

@interface XJPLPlayerFactory : NSObject<FlutterPlatformViewFactory>

- (instancetype)initWithMessager:(id<FlutterBinaryMessenger>)messager;
@end

NS_ASSUME_NONNULL_END

XJPLPlayerFactory.m

//
//  XJPLPlayerFactory.m
//  plugin_plplayer
//
//  Created by xj_mac on 2020/12/24.
//

#import "XJPLPlayerFactory.h"
#import "XJPLPlayerView.h"
@interface XJPLPlayerFactory ()

@property (nonatomic,weak)  id <FlutterBinaryMessenger> messager;

@end

@implementation XJPLPlayerFactory


- (instancetype)initWithMessager:(id<FlutterBinaryMessenger>)messager {
    self = [super init];
    if (self) {
        self.messager = messager;
    }
    return self;
}

- (NSObject<FlutterPlatformView>*)createWithFrame:(CGRect)frame
                                   viewIdentifier:(int64_t)viewId
                                        arguments:(id _Nullable)args {
    return [[XJPLPlayerView alloc]initWithFrame:frame viewIdentifier:viewId arguments:args messager:self.messager];
}

- (NSObject<FlutterMessageCodec> *)createArgsCodec {
    return [FlutterStandardMessageCodec sharedInstance];
}

@end