@interface WebCell ()@property(nonatomic, weak) UIScrollView*webScrollView;
@property(nonatomic, weak) WKWebView*webView;@end
@implementationWebCell- (UIScrollView *)webScrollView
{if (!_webScrollView) {
UIScrollView*sv =[[UIScrollView alloc] init];
[self.contentView addSubview:sv];
_webScrollView=sv;
}return_webScrollView;
}- (WKWebView *)webView
{if (!_webView) {//配置环境
WKWebViewConfiguration *configuration =[[WKWebViewConfiguration alloc] init];//自适应屏幕宽度js
NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript*wkUserScript =[[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];//允许视频播放
if (iOS_Version(9.0)) {
configuration.allowsAirPlayForMediaPlayback=YES;
}//允许在线播放
configuration.allowsInlineMediaPlayback =YES;//允许与网页交互
configuration.selectionGranularity =YES;//内容处理池
configuration.processPool =[[WKProcessPool alloc] init];//自定义配置,一般用于js调用oc方法(oc拦截URL中的数据做自定义操作)
WKUserContentController *userContentController =[[WKUserContentController alloc] init];//添加消息处理,注意:self指代的对象需要遵守WKScriptMessageHandler协议,结束时需要移除
[userContentController addScriptMessageHandler:self name:@"Handle"];
[userContentController addUserScript:wkUserScript];//是否支持记忆读取
configuration.suppressesIncrementalRendering =YES;//允许用户更改网页的设置
configuration.userContentController =userContentController;//创建wk
WKWebView *wv =[[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];//设置背景色
wv.backgroundColor =[UIColor clearColor];
wv.opaque=NO;//设置代理
wv.navigationDelegate =self;
wv.UIDelegate=self;//网页内容禁用滑动
wv.scrollView.scrollEnabled =NO;//kvo添加进度监控//[wv addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil];//开启手势触摸
wv.allowsBackForwardNavigationGestures =YES;//自适应
[wv sizeToFit];
[self.webScrollView addSubview:wv];
_webView=wv;
}return_webView;
}#pragma mark -
- (void)setModel:(Model *)model
{
_model=model;//手动改变图片适配问题,拼接html代码后,再加载html代码
NSString *myStr = [NSString stringWithFormat:@"
", SCREEN_WIDTH - 20];NSString*str = [NSString stringWithFormat:@"%@%@",myStr, model.htmlStr];
[self.webView loadHTMLString:str baseURL:nil];
}#pragma mark -
- (void)layoutSubviews
{
[super layoutSubviews];
[self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.left.right.mas_equalTo(self.contentView);
}];
}#pragma mark -
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
[webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable response, NSError *_Nullable error) {//获取webView的高度
CGFloat webViewHeight =[response floatValue];//设置自定义scrollView的frame
self.webScrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, webViewHeight);//通过代理方法,刷新表格高度
if ([self.delegaterespondsToSelector:@selector(webViewWithChangeHeight:)]) {
[self.delegatewebViewWithChangeHeight:webViewHeight];
}
}];
}- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{}- (void)dealloc
{
[self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"Handle"];
}@end