iOS-WKWebview 带有进度条加载的ViewController【KVO监听Webview加载进度】

前言
为什么要说 WKWebview,在之前做电子书笔记时已经提过 WKWebview 在iOS8之后已完全替代 Webview,原因就不多说了,主要还是内存过大;
封装
封装一个基于 UIViewController 类: WKWebViewController
WKWebViewController.h
@interface WKWebViewController : UIViewController
///目标URL
@property (nonatomic,strong) NSString *url;
@property (nonatomic,strong) WKWebView *webview;
@end
WKWebViewController.m
#import "WKWebViewController.h" @interface WKWebViewController ()<WKNavigationDelegate,UIScrollViewDelegate>
///进度条
@property (nonatomic, strong) UIProgressView *progressView;
@end @implementation WKWebViewController - (void)viewDidLoad {
[super viewDidLoad];
///添加 KVO 对进度监听
[self.webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
[self.view addSubview:self.webview];
[self.view addSubview:self.progressView]; } #pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"estimatedProgress"]) {
self.progressView.progress = self.webview.estimatedProgress;
if (self.progressView.progress == ) {
__weak typeof (self)weakSelf = self;
[UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
} completion:^(BOOL finished) {
weakSelf.progressView.hidden = YES; }];
}
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
} #pragma mark - webview 代理
//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
NSLog(@"开始加载网页");
//开始加载网页时展示出progressView
self.progressView.hidden = NO;
//开始加载网页的时候将progressView的Height恢复为1.5倍
self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
//防止progressView被网页挡住
[self.view bringSubviewToFront:self.progressView];
}
//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
NSLog(@"加载完成");
//加载完成隐藏progressView
self.progressView.hidden = YES;
}
//加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
NSLog(@"加载失败");
//加载失败隐藏progressView
self.progressView.hidden = YES;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return nil;
}
#pragma mark - 属性
- (WKWebView *)webview{
if (!_webview) {
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
// 自适应屏幕宽度js
NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; // 添加自适应屏幕宽度js调用的方法
[wkWebConfig.userContentController addUserScript:wkUserScript];
_webview = [[WKWebView alloc]initWithFrame:CGRectMake(, , self.view.bounds.size.width, self.view.bounds.size.height - (wkj_IsIphoneX ? + : )) configuration:wkWebConfig];
_webview.navigationDelegate = self;
_webview.scrollView.delegate = self;
_webview.opaque = NO;
}
return _webview;
}
- (UIProgressView *)progressView{
if (!_progressView) {
_progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(, , [[UIScreen mainScreen] bounds].size.width, )];
_progressView.trackTintColor = [ColorKLSystem colorWithAlphaComponent:0.5];
_progressView.tintColor = ColorKLSystem;
_progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
}
return _progressView;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
使用
新建一个基于WKWebViewController的VC,设置URL,然后加载就可以了
iOS-WKWebview 带有进度条加载的ViewController【KVO监听Webview加载进度】的更多相关文章
- js - 预加载+监听图片资源加载制作进度条
这两天遇到一个新需求:一个一镜到底的h5动画.因为功能的特殊性,就要求我们提前监听页面的静态图片是否全部加载完毕.即处理预加载. 总结下来,下次这种需求需要提前注意以下几点: 一.图片而不是背景图 本 ...
- jQuery学习(监听DOM加载)
jQuery的extend方法 function njQuery() { } /* njQuery.extend = function (obj) { // 此时此刻的this就是njQuery这个类 ...
- C# NanUI WinFormium监听页面加载开始\结束
个人博客 地址:https://www.wenhaofan.com/article/20190501213608 因为NanUI文档中仅介绍了Formium窗口的监听,但是没有WinFormium相关 ...
- iOS: 使用KVO监听控制器中数组的变化
一.介绍: KVO是一种能动态监听到属性值的改变的方式,使用场景非常广泛,这里我只讲如何监听控制器ViewController中数组的变化. 二.了解: 首先我们应该知道KVO是不能直接监听控制器Vi ...
- SpringMVC 监听文件上传进度
Spring MVC 监听文件上传进度 具体实现分三个步骤: 接管CommonsMultipartResolver,重写针对文件上传的请求. 在第一步中写入监听,以获取上传进度. 修改上传部分的配置文 ...
- 李洪强iOS开发本人集成环信的经验总结_07_监听好友请求
李洪强iOS开发本人集成环信的经验总结_07_监听好友请求 来到Appdalegate中: 遵守代理协议 设置代理 实现监听好友请求的回调的方法
- 监听spring加载完成后事件
有这个想法是在很早以前了,那时的我没有接触什么缓存技术,只知道hibernate有个二级缓存.没有用过memcache,也没有使用过redis. 只懂得将数据放到数组里或者集合里,一直不去销毁它(只有 ...
- Android中监听webview监听是否加载完成
之前写过一篇捕获Phoengap的webview事件的方法,主要是在实现了CordovaInterface的Activity中, 在onMessage中根据第一个参数的message name来判断 ...
- 【winform】基于UserControl实现webBrower组件时html页面元素加载及onclick事件监听实现
[背景]基于System.Windows.Forms.UserControl实现的webBrower组件在html内使用window.external调用winform事件失败. [解决思路]借助wi ...
随机推荐
- jQuery load和unload函数使用
实例 当用户点击链接离开本页时,弹出一个消息框: $(window).unload(function(){ alert("Goodbye!"); }); 亲自试一试 定义和用法 当 ...
- centos7 rabbitmq安装以及应用
安装单机rabbitmq 1.安装erlang cd /usr.local yum install wget yum install net-tools wget http://erlang.or ...
- 2018.07.28 uoj#169. 【UR #11】元旦老人与数列(线段树)
传送门 线段树好题. 维护区间加,区间取最大值,维护区间最小值,历史区间最小值. 同样先考虑不用维护历史区间最小值的情况,这个可以参考这道题的解法,维护区间最小和次小值可以解决前两个操作,然后使用历史 ...
- 使用ntpdate校正linux系统的时间
当Linux服务器的时间不对的时候,可以使用ntpdate工具来校正时间. 安装:yum install ntpdate ntpdate简单用法: # ntpdate ip # ntpdate 210 ...
- Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable t
spring与hibernate整合然后出现如下错误: org.springframework.beans.factory.BeanCreationException: Error creating ...
- cxf-rs client 调用
org.apache.cxf.jaxrs.client.WebClient get调用 @GET @Path("/echo/{input}") @Produces("te ...
- day06(Collection,List,ArrayList,LinkedList,iterator迭代器,增强for)
Collection 接口 方法实现 boolean add(Object o);//添加 boolean remove(Object o);//移除 修改方法 让实现类自己去实现修 ...
- 如何使用C++11实现C#属性概念设计
目录(原创博客,版权所有,转载请注明出处 http://www.cnblogs.com/feng-sc) 1.概述 2.C#属性的概念 2.1.简单示例代码介绍C#中的属性 2.2.C++实现效果 ...
- CodeForces - 589J —(DFS)
Masha has recently bought a cleaner robot, it can clean a floor without anybody's assistance. Schema ...
- 深入理解BS结构应用程序
随着学习的深入,和编程经验的丰富,对BS应用程序有一些认识. 在一些讨论软件技术的QQ群里,或一些社区.BBS中,经常会有一些初学者会犯一些认知性的错误.比如经常会有一些朋友提这样的一些问题:“我怎么 ...