UIWebView自iOS2就有金句,WKWebView從iOS8才有盒揉,毫無(wú)疑問(wèn)WKWebView將逐步取代笨重的UIWebView前计。通過(guò)簡(jiǎn)單的測(cè)試即可發(fā)現(xiàn)UIWebView占用過(guò)多內(nèi)存曲掰,且內(nèi)存峰值更是夸張。WKWebView網(wǎng)頁(yè)加載速度也有提升酬屉,但是并不像內(nèi)存那樣提升那么多
WKWebView優(yōu)勢(shì):
占用內(nèi)存可能只有 UIWebView 的1/3~1/4
更多的支持HTML5的特性
官方宣稱的高達(dá)60fps的滾動(dòng)刷新率以及內(nèi)置手勢(shì)
Safari相同的JavaScript引擎
將UIWebViewDelegate與UIWebView拆分成了14類與3個(gè)協(xié)議(官方文檔說(shuō)明)
另外用的比較多的半等,增加加載進(jìn)度屬性:estimatedProgress
iOS網(wǎng)絡(luò)—UIWebView、WKWebView使用詳解
說(shuō)下怎么使用增加加載進(jìn)度屬性
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];
}