先提供demo,目錄如下圖
主要步驟(下面會(huì)具體介紹)
一、首先讓flutter項(xiàng)目可以顯示ios控件
flutter提供了一個(gè)AndroidView(android),UiKitView(ios)的控件,來(lái)實(shí)現(xiàn)原生顯示在flutter上
1、ios端 info.plist文件設(shè)置;
2、ios端 FlutterPlatformView代理方法- (UIView*)view;提供原生view;
3、 ios 端 FlutterPlatformViewFactory代理方法
- (NSObject<FlutterPlatformView>*)createWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
arguments:(id _Nullable)args;
生成FlutterPlatformView
4、 ios 端FlutterPlugin代理方法
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar;
注冊(cè)原生view
5、 flutter 通過(guò)UiKitView嵌入 原生view
二、ios頁(yè)面里封裝WKWebView
具體介紹:
1、ios端 info.plist文件設(shè)置:io.flutter.embedded_views_preview為yes
2、ios端 FlutterPlatformView代理方法- (UIView*)view;提供原生view;
3、 ios 端 FlutterPlatformViewFactory代理方法生成FlutterPlatformView
4、 ios 端FlutterPlugin代理方法注冊(cè)原生view
5、 flutter 通過(guò)UiKitView嵌入 原生view
封裝wkwebview,就屬于原生的了,怎么寫就很簡(jiǎn)單了
我這里只簡(jiǎn)單顯示了下
//
// WGFlutterIosWebView.m
// Runner
//
// Created by wanggang on 2019/8/19.
// Copyright ? 2019 The Chromium Authors. All rights reserved.
//
#import "WGFlutterIosWebView.h"
#import <WebKit/WebKit.h>
@interface WGFlutterIosWebView()<WKNavigationDelegate,WKUIDelegate>
//WGFlutterIosWebView 創(chuàng)建后的標(biāo)識(shí)
@property (nonatomic, assign) NSInteger viewId;
@property (nonatomic, strong) WKWebView *wkWebView;
@end
@implementation WGFlutterIosWebView
-(instancetype)initWithWithFrame:(CGRect)frame
viewIdentifier:(NSInteger)viewId
arguments:(id _Nullable)args
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger{
self = [super init];
if (self) {
if (frame.size.width==0) {
frame=CGRectMake(frame.origin.x, frame.origin.y, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}
self.wkWebView = [[WKWebView alloc] initWithFrame:frame];
_wkWebView.navigationDelegate = self;
_wkWebView.UIDelegate = self;
//接收 初始化參數(shù)
NSDictionary *dic = args;
NSString *content = dic[@"content"];
if (content!=nil) {
[_wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:content]]];
}
_viewId = viewId;
}
return self;
}
- (nonnull UIView *)view {
return self.wkWebView;
}
@end