UIWebView自iOS2就有,WKWebView从iOS8才有,毫无疑问WKWebView将逐步取代笨重的UIWebView。通过简单的测试即可发现UIWebView占用过多内存,且内存峰值更是夸张。WKWebView网页加载速度也有提升,但是并不像内存那样提升那么多
WKWebView优势:
占用内存可能只有 UIWebView 的1/3~1/4
更多的支持HTML5的特性
官方宣称的高达60fps的滚动刷新率以及内置手势
Safari相同的JavaScript引擎
将UIWebViewDelegate与UIWebView拆分成了14类与3个协议(官方文档说明)
另外用的比较多的,增加加载进度属性:estimatedProgress
说下怎么使用增加加载进度属性
estimatedProgress
,直接上代码:
- (void)viewDidLoad {
[super viewDidLoad];
_webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
_webView.UIDelegate = self;
_webView.navigationDelegate = self;
[self.view addSubview:_webView];
_progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 65, CGRectGetWidth(self.view.frame),2)];
[self.view addSubview:_progressView];
[_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld context:nil];
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
NSLog(@" %s,change = %@",__FUNCTION__,change);
if ([keyPath isEqual: @"estimatedProgress"] && object == _webView) {
[self.progressView setAlpha:1.0f];
[self.progressView setProgress:_webView.estimatedProgress animated:YES];
if(_webView.estimatedProgress >= 1.0f)
{
[UIView animateWithDuration:0.3 delay:0.3 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];
}
}
- (void)dealloc {
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
// if you have set either WKWebView delegate also set these to nil here
[_webView setNavigationDelegate:nil];
[_webView setUIDelegate:nil];
}
网友评论