wkwebview替换uiwebview_iOS项目之WKWebView替换UIWebView相关

@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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值