1、前言
iOS8后塞耕,蘋果推出了新框架Webkit蚀腿,提供了替換UIWebView的組件WKWebView,相比于UIWebView,好處多多扫外,速度更快了莉钙,占用內(nèi)存少了。
2筛谚、基本使用
使用方法和UIWebView大同小異磁玉,具體使用可以參考使用WKWebView替換UIWebView,這篇文章講的挺詳細(xì)了,這不是我們本篇的重點(diǎn)驾讲,我們的重點(diǎn)是講下怎么頁(yè)面title和加載進(jìn)度值蚊伞。
3、獲得頁(yè)面title和加載進(jìn)度值(基于系統(tǒng)KVO)
//導(dǎo)入框架
#import <WebKit/WebKit.h>
/*********/
@property (nonatomic, strong) WKWebView *mWebView;//webView
@property (nonatomic, strong) UIProgressView *mProgressView;//進(jìn)度條
/*********/
- (void)viewDidLoad {
[super viewDidLoad];
[self initWebView];
}
//增加kvo監(jiān)聽(tīng)吮铭,獲得頁(yè)面title和加載進(jìn)度值
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.mWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
[self.mWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
}
//KVO 一定要移除时迫,要不然會(huì)崩潰
- (void)dealloc{
[self.mWebView removeObserver:self forKeyPath:@"estimatedProgress"];
[self.mWebView removeObserver:self forKeyPath:@"title"];
}
- (void)initWebView
{
_mWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - TopFullHeight)];
[_mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http:xxxxx"]]];
[self.view addSubview:_mWebView];
//進(jìn)度條添加到navigationBar
CGFloat progressBarHeight = 2.0f;
CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height - progressBarHeight, navigationBarBounds.size.width, progressBarHeight);
_mProgressView = [[UIProgressView alloc] initWithFrame:barFrame];
_mProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
_mProgressView.progressTintColor = [UIColor greenColor];
[self.navigationController.navigationBar addSubview:_mProgressView];
}
#pragma mark KVO的監(jiān)聽(tīng)代理
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
//加載進(jìn)度值
if ([keyPath isEqualToString:@"estimatedProgress"]){
if (object == self.mWebView){
self.mProgressView.alpha = 1;
[self.mProgressView setProgress:self.mWebView.estimatedProgress animated:YES];
if(self.mWebView.estimatedProgress >= 1.0f)
{
[UIView animateWithDuration:0.5 animations:^{
self.mProgressView.alpha = 0;
} completion:^(BOOL finished) {
[self.mProgressView setProgress:0.0f animated:NO];
}];
}
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}else if ([keyPath isEqualToString:@"title"]){//網(wǎng)頁(yè)title
if (object == self.mWebView){
self.navigationItem.title = self.mWebView.title;
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
至此,加載標(biāo)題和進(jìn)度值完成谓晌,try掠拳。
4、基于 KVOController
KVOController是facebook開(kāi)源的一個(gè)KVO框架纸肉,使用方便溺欧,好處多多,我就是因?yàn)閭?cè)滑返回偶爾導(dǎo)致系統(tǒng)KVO沒(méi)有釋放毁靶,最后程序崩潰了胧奔,所以這里建議使用這個(gè)框架,使用代碼入下
#import "FBKVOController.h"
@property (nonatomic, strong) FBKVOController *kvoController;
- (void)initFBKVO{
//KVO
__weak typeof (self) weakSelf = self;
self.kvoController = [FBKVOController controllerWithObserver:self];
[self.kvoController observe:self.mWebView keyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
weakSelf.mProgressView.alpha = 1;
[weakSelf.mProgressView setProgress:self.mWebView.estimatedProgress animated:YES];
if(weakSelf.mWebView.estimatedProgress >= 1.0f)
{
[UIView animateWithDuration:0.5 animations:^{
weakSelf.mProgressView.alpha = 0;
} completion:^(BOOL finished) {
[weakSelf.mProgressView setProgress:0.0f animated:NO];
}];
}
}];
[self.kvoController observe:self.mWebView keyPath:@"title" options:NSKeyValueObservingOptionNew block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
weakSelf.navigationItem.title = self.mWebView.title;
}];
}
參考
http://www.reibang.com/p/6f2d733502c6
http://blog.csdn.net/reylen/article/details/46679895