#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,查找了相关文章,找到了一份非常...