#import "WebHelpCtrl.h"
#import <WebKit/WebKit.h>
@interface WebHelpCtrl ()<WKUIDelegate,WKNavigationDelegate>
@property(nonatomic,strong)WKWebView *webView;
@property(nonatomic,strong)UIProgressView *progress;
@end
@implementation WebHelpCtrl
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.webView];
// Do any additional setup after loading the view from its nib.
}
- (WKWebView *)webView
{
if (_webView == nil)
{
_webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
_webView.UIDelegate = self;
_webView.navigationDelegate = self;
_webView.backgroundColor = [UIColor clearColor];
[self.view addSubview:_webView];
}
return _webView;
}
#pragma mark 加载进度条
- (UIProgressView *)progress
{
if (_progress == nil)
{
_progress = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 2)];
_progress.tintColor = [UIColor blueColor];
_progress.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_progress];
}
return _progress;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//TODO:加载
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
//TODO:kvo监听,获得页面title和加载进度值
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
[self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
}
#pragma mark KVO的监听代理
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
//加载进度值
if ([keyPath isEqualToString:@"estimatedProgress"])
{
if (object == self.webView)
{
[self.progress setAlpha:1.0f];
[self.progress setProgress:self.webView.estimatedProgress animated:YES];
if(self.webView.estimatedProgress >= 1.0f)
{
[UIView animateWithDuration:0.5f
delay:0.3f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self.progress setAlpha:0.0f];
}
completion:^(BOOL finished) {
[self.progress setProgress:0.0f animated:NO];
}];
}
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
//网页title
else if ([keyPath isEqualToString:@"title"])
{
if (object == self.webView)
{
self.title = self.webView.title;
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#pragma mark 移除观察者
- (void)dealloc
{
_webView.UIDelegate = nil;
_webView.navigationDelegate = nil;
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
[_webView removeObserver:self forKeyPath:@"title"];
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]]];
_webView = nil;
}
iOS WKWebView 获得title和进度
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 关于WKWebview与UIWebview的简单对比,可以参考我去年写的一篇文章WKWbeview的初步使用。这次...
- UIWebView获取title的方式 WKWebView获取title的方式比较复杂点 1.wkWebView初...
- WKWebView的进度条WKWebView的回退上级网页WKWebView的获取网页标题 [获取系统返回按钮的点...
- 最近写项目的时候,有一个需求,要加载HTML页面,并且显示HTML页面的title,查找了相关文章,找到了一份非常...