WKWebView 相比UIWebView 在稳定性,性能,功能方面有很大提升 (战友内存比webView小,缺点 不支持缓存和NSURLProtocol)增加了加载进度条
头文件 WebKit/WebKit.h这里写代码片
// 创建一个webiview的配置项
WKWebViewConfiguration *configuretion = [[WKWebViewConfiguration alloc] init];
// 设置偏好设置
configuretion.preferences = [[WKPreferences alloc]init];
configuretion.preferences.minimumFontSize = 10;
configuretion.preferences.javaScriptEnabled = true;
configuretion.processPool = [[WKProcessPool alloc]init];
// 通过js与webview内容交互配置
configuretion.userContentController = [[WKUserContentController alloc] init];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
用来注册JS里面 的方法名 来接收js里面触发的回调事件
//OC注册供JS调用的方法
[configuretion.userContentController addScriptMessageHandler:self name:@"linkToItem"];
[configuretion.userContentController addScriptMessageHandler:self name:@"callHeight"];
// 默认是不能通过JS自动打开窗口的,必须通过用户交互才能打开
configuretion.preferences.javaScriptCanOpenWindowsAutomatically = NO;
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(Bounds), CGRectGetHeight(Bounds)) configuration:configuretion];//self.view.bounds
if (self.webUrl.length > 0) {
[_webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.webUrl]]];
} else {
[_webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://lushanghulian.com/Api/event"]]];
}
[_webView goBack];
[_webView goForward];
_webView.navigationDelegate = self;
_webView.UIDelegate = self;
[self.view addSubview:_webView];
WKWebView 的代
//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
[self showHUDWithHint:@""];
}
加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[self hideHud];
//获得高度
// [webView evaluateJavaScript:@"document.body.offsetHeight;" completionHandler:^(id Result, NSError * error) {
//
// }];
}
加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation
withError:(NSError *)error {
[self hideHud];
}
回调事件 message.body 传递参数
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
NSLog(@"方法名:%@", message.name);
NSLog(@"参数:%@", message.body);
}
如何给webView的加载进度
通过监听webView estimatedProgress 属性
[_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
移除监听
- (void)dealloc {
[self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}
//监听的代理
//UIProgressView 这个自己写一个进度条就可以根据监听改变就行
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == self.webView && [keyPath isEqualToString:@"estimatedProgress"]) {
CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
if (newprogress == 1) {
self.progressView.hidden = YES;
[self.progressView setProgress:0 animated:NO];
}else {
self.progressView.hidden = NO;
[self.progressView setProgress:newprogress animated:YES];
}
}
}