前言:之前用的UIWebView,自从8.0WKWebView出来以后,不仅内存更为优化,滑动更为流畅之外,苹果还提供了丰富的类库,最近也是应公司跨平台要求,找了许久资料,算是做了一个小封装。当然很多朋友会说,那我没有html去调用,也不会写js怎么办,这边我给大家准备了一个写好的html文件,拖入工程即可使用,
《封装BaseWebViewController下载链接》
封装功能包括,进度条显示,加载本地与外部网页,js交互。
至于14个类,三套协议就不多说了(其实是不愿意去看……)那么介绍一下实用的一些方法吧。
1.头文件导入 WebKit/WebKit.h,使用WKWebView必须依赖这个类。
2.三个协议 WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler
3.我使用懒加载实现了webView和进度条progressView的加载
#pragma mark - 懒加载
- (WKWebView *)wkWebView
{
if (!_wkWebView) {
//设置网页的配置文件
WKWebViewConfiguration * Configuration = [[WKWebViewConfiguration alloc]init];
// 允许可以与网页交互,选择视图
Configuration.selectionGranularity = YES;
// web内容处理池
Configuration.processPool = [[WKProcessPool alloc] init];
//自定义配置,一般用于 js调用oc方法(OC拦截URL中的数据做自定义操作)
WKUserContentController * UserContentController = [[WKUserContentController alloc]init];
// 添加消息处理,注意:self指代的对象需要遵守WKScriptMessageHandler协议,结束时需要移除
[UserContentController addScriptMessageHandler:self name:@"webViewApp"];
// 是否支持记忆读取
Configuration.suppressesIncrementalRendering = YES;
// 允许用户更改网页的设置
Configuration.userContentController = UserContentController;
_wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:Configuration];
_wkWebView.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0];
// 设置代理
_wkWebView.navigationDelegate = self;
_wkWebView.UIDelegate = self;
//kvo 添加进度监控
[_wkWebView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:WkwebBrowserContext];
//开启手势触摸
_wkWebView.allowsBackForwardNavigationGestures = YES;
// 设置 可以前进 和 后退
//适应你设定的尺寸
[_wkWebView sizeToFit];
[self.view addSubview:self.wkWebView];
}
return _wkWebView;
}
//isNavHidden这边是做了一个是否隐藏导航栏的功能,具体下载代码一看便知。
- (UIProgressView *)progressView
{
if (!_progressView) {
_progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
if (_isNavHidden == YES) {
_progressView.frame = CGRectMake(0, 20, self.view.bounds.size.width, 3);
}else{
_progressView.frame = CGRectMake(0, 64, self.view.bounds.size.width, 3);
}
// 设置进度条的色彩
[_progressView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
_progressView.progressTintColor = [UIColor blackColor];
}
return _progressView;
}
4.当然进度条有了,必然要有进度条监听的实现
//KVO监听进度条
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.wkWebView) {
[self.progressView setAlpha:1.0f];
BOOL animated = self.wkWebView.estimatedProgress > self.progressView.progress;
[self.progressView setProgress:self.wkWebView.estimatedProgress animated:animated];
// Once complete, fade out UIProgressView
if(self.wkWebView.estimatedProgress >= 1.0f) {
[UIView animateWithDuration:0.3f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.progressView setAlpha:0.0f];
} completion:^(BOOL finished) {
[self.progressView setProgress:0.0f animated:NO];
}];
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
5.如果拦截网页中JS给我们传递的消息,用一个block回传
#pragma mark WKScriptMessageHandler
//拦截执行网页中的JS方法
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
//服务器固定格式写法 window.webkit.messageHandlers.名字.postMessage(内容);
//客户端写法 message.name isEqualToString:@"名字"]
if (_callJsBackWithBodyBlock) {
self.callJsBackWithBodyBlock(message.body, message.name);
}
}
6.给JS传递一些值
//callJS可以实现回传,在需要的类调用这个方法即可
- (void)callJS:(NSString *)jsMethod handler:(void (^)(id _Nullable))handler {
NSLog(@"call js:%@",jsMethod);
[self.wkWebView evaluateJavaScript:jsMethod completionHandler:^(id _Nullable response, NSError * _Nullable error) {
if (handler) {
handler(response);
}
}];
}