iOS WKWebView 获得title和加载进度
来源:互联网 发布:股票智能分析软件 编辑:程序博客网 时间:2024/05/21 10:13
1、前言
iOS8后,苹果推出了新框架Webkit,提供了替换UIWebView的组件WKWebView,相比于UIWebView,好处多多,速度更快了,占用内存少了。
2、基本使用
使用方法和UIWebView大同小异,具体使用可以参考使用WKWebView替换UIWebView,这篇文章讲的挺详细了,这不是我们本篇的重点,我们的重点是讲下怎么页面title和加载进度值。
3、获得页面title和加载进度值(基于系统KVO)
//导入框架#import <WebKit/WebKit.h>/*********/@property (nonatomic, strong) WKWebView *mWebView;//webView@property (nonatomic, strong) UIProgressView *mProgressView;//进度条/*********/- (void)viewDidLoad { [super viewDidLoad]; [self initWebView];} //增加kvo监听,获得页面title和加载进度值- (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 一定要移除,要不然会崩溃- (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]; //进度条添加到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的监听代理- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { //加载进度值 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"]){//网页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]; }}
至此,加载标题和进度值完成,try。
4、基于 KVOController
KVOController是facebook开源的一个KVO框架,使用方便,好处多多,我就是因为侧滑返回偶尔导致系统KVO没有释放,最后程序崩溃了,所以这里建议使用这个框架,使用代码入下
#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.jianshu.com/p/6f2d733502c6
http://blog.csdn.net/reylen/article/details/46679895
阅读全文
0 0
- iOS WKWebView 获得title和加载进度
- WKWebView进度及title
- WKWebView进度及title
- WKWebView进度及title
- iOS WKWebView中显示进度条及HTML的title
- iOS WKWebView添加网页加载进度条
- iOS 封装WKWebView页面带加载进度条
- iOS 8 WkWebView 网页的配置和前进,后退,js 交互和进度条的加载
- iOS 8 WkWebView 网页的配置和前进,后退,js 交互和进度条的加载
- iOS 介绍使用UIWebView和WKWebView加载进度条的代码+demo
- IOS 使用 WKWebView 加载本地的JS和CSS文件出现乱码问题
- iOS ActivityIndlcatorView(加载进度轮)
- UIWebview和WKWebview加载本地html
- iOS 加载HTML的相关问题(与JS的互相调用和WKWebView加载本地网页)
- IOS WKWebView
- 【iOS】WKWebView
- iOS - WKWebView
- IOS-WKWebView
- Android PorterDuff.Mode 的介绍
- Hibernate中createCriteria即QBC查询的详细用法和HQL的区别?
- 【剧透】2017云栖大会•北京峰会——企业云上业务优化专场
- disconf配置项获取不到值
- 约瑟夫环的C语言链表实现
- iOS WKWebView 获得title和加载进度
- vue成长之路+实战+Vue2+VueRouter2+webpack(一)vue-router路由入门
- Linux搭建使用LDAP协议认证的SVN服务器
- C++发展简史
- 几步教你怎么写MVP
- 来到CSDN的第一次
- 软件调试笔记51
- linux 下tar使用方法
- Hex、bin、axf、elf格式文件小结