iOS UIWebView 加载进度条的使用-WKWebView的使用,更新2017.6.26
1、由于项目中加载网络插件,直接使用了webview加载。使用了三方NJKWebViewProgress进度条的使用,近期在测试时发现,网络缓慢时出现白屏,有卡顿现象。
于是采用了WKWebView进行加载,KVO监听下载进度
// ViewController.m #import "ViewController.h" #import <WebKit/WebKit.h>
#import <WebKit/WKWebView.h> #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height) #define IPHONEHIGHT(b) [UIScreen mainScreen].bounds.size.height*((b)/1334.0)
#define IPHONEWIDTH(a) [UIScreen mainScreen].bounds.size.width*((a)/750.0) static void *WkwebBrowserContext = &WkwebBrowserContext;
@interface ViewController ()<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler,UINavigationControllerDelegate,UINavigationBarDelegate>
{
UIButton * back;
UILabel * labeltitle;//webview显示前的title }
@property (nonatomic, strong) WKWebView *wkWebView;
//设置加载进度条
@property (nonatomic,strong) UIProgressView *progressView;
@end @implementation ViewController -(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated]; self.tabBarController.tabBar.hidden = YES;
self.navigationController.navigationBar.hidden= NO;
[[UIApplication sharedApplication]setStatusBarHidden:YES]; }
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.tabBarController.tabBar.hidden = NO;
self.navigationController.navigationBar.hidden= NO;
[[UIApplication sharedApplication]setStatusBarHidden:NO]; [self.wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"WXPay"];
[self.wkWebView setNavigationDelegate:nil];
[self.wkWebView setUIDelegate:nil]; //清除缓存
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:
@"https://www.baidu.com"]]]; } - (void)viewDidLoad { [super viewDidLoad]; [self CreatUI]; }
-(void)CreatUI{ self.view.backgroundColor = [UIColor blackColor];
[self.wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]]; //添加到主控制器上 [self.view addSubview:self.wkWebView];
//添加进度条
[self.view addSubview:self.progressView];
//
// back = [myButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(ScreenWidth-IPHONEWIDTH(120), ScreenHeight-IPHONEHIGHT(100), IPHONEWIDTH(50), IPHONEHIGHT(50)) tag:1 image:@"ic_history_ct_return" andBlock:^(myButton *button) {
//
// [self.navigationController popViewControllerAnimated:YES];
//
//
// }];
//
// [self.view addSubview:back];
// labeltitle = [[UILabel alloc] initWithFrame:CGRectMake(, IPHONEHIGHT(), self.view.bounds.size.width, IPHONEHIGHT())];
//设置成绿色
labeltitle.backgroundColor = [UIColor blackColor];
labeltitle.text =@"Title"; labeltitle.textColor = [UIColor whiteColor];
labeltitle.font = [UIFont boldSystemFontOfSize:IPHONEHIGHT()];
labeltitle.textAlignment = NSTextAlignmentCenter; [self.view addSubview:labeltitle]; // [MBProgressHUD showHUDAddedTo:self.view animated:YES]; } #pragma mark ================ WKNavigationDelegate ================ //这个是网页加载完成,导航的变化
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
[labeltitle removeFromSuperview];
// [MBProgressHUD hideHUDForView:self.view animated:YES];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; } //开始加载
-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
//开始加载的时候,让加载进度条显示
self.progressView.hidden = NO; } //内容返回时调用
-(void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{} //服务器请求跳转的时候调用
-(void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{} // 内容加载失败时候调用
-(void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error{
NSLog(@"页面加载超时");
// [MBProgressHUD hideHUDForView:self.view animated:YES]; // [MBProgressHUD showText:@"加载失败,请重试" HUDAddedTo:self.view animated:YES afterDelay:1.5];
} //跳转失败的时候调用
-(void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{} //进度条
-(void)webViewWebContentProcessDidTerminate:(WKWebView *)webView{} //KVO监听进度条
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.wkWebView) {
[self.progressView setAlpha:1.0f];
BOOL animated = self.wkWebView.estimatedProgress > self.progressView.progress;
[self.progressView setProgress:self.wkWebView.estimatedProgress animated:animated]; // Once complete, fade out UIProgressView
if(self.wkWebView.estimatedProgress >= 1.0f) {
[UIView animateWithDuration:0.3f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.progressView setAlpha:0.0f];
} completion:^(BOOL finished) {
[self.progressView setProgress:0.0f animated:NO];
}];
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
} #pragma mark ================ 懒加载 ================ - (WKWebView *)wkWebView{
if (!_wkWebView) {
//设置网页的配置文件
WKWebViewConfiguration * Configuration = [[WKWebViewConfiguration alloc]init];
//允许视频播放
Configuration.allowsAirPlayForMediaPlayback = YES;
// 允许在线播放
Configuration.allowsInlineMediaPlayback = YES;
// 允许可以与网页交互,选择视图
Configuration.selectionGranularity = YES;
// web内容处理池
Configuration.processPool = [[WKProcessPool alloc] init];
//自定义配置,一般用于 js调用oc方法(OC拦截URL中的数据做自定义操作)
WKUserContentController * UserContentController = [[WKUserContentController alloc]init];
// 添加消息处理,注意:self指代的对象需要遵守WKScriptMessageHandler协议,结束时需要移除
[UserContentController addScriptMessageHandler:self name:@"WXPay"];
// 是否支持记忆读取
Configuration.suppressesIncrementalRendering = YES;
// 允许用户更改网页的设置
Configuration.userContentController = UserContentController;
_wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:Configuration];
_wkWebView.backgroundColor = [UIColor blackColor];
// 设置代理
_wkWebView.navigationDelegate = self;
_wkWebView.UIDelegate = self;
//kvo 添加进度监控
[_wkWebView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options: context:WkwebBrowserContext];
//开启手势触摸
_wkWebView.allowsBackForwardNavigationGestures = YES;
// 设置 可以前进 和 后退
//适应你设定的尺寸
[_wkWebView sizeToFit];
}
return _wkWebView;
} - (UIProgressView *)progressView{
if (!_progressView) {
_progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault]; _progressView.frame = CGRectMake(, IPHONEHIGHT(), self.view.bounds.size.width, IPHONEHIGHT()); // 设置进度条的色彩
[_progressView setTrackTintColor:[UIColor colorWithRed:240.0/ green:240.0/ blue:240.0/ alpha:1.0]];
_progressView.progressTintColor = [UIColor greenColor];
}
return _progressView;
} //注意,观察的移除
-(void)dealloc{
[self.wkWebView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
} @end
不建议采用下方代码,会在网络缓慢时有白屏现象
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1/拖到自己的
iOS UIWebView 加载进度条的使用-WKWebView的使用,更新2017.6.26的更多相关文章
- iOS7 UIWebview加载进度条实现
不同于WKWebview,wk是有自己的加载进度值的,我们可以直接通过kvo检测到,并显示到进度条内. 但如果我们为了适配ios7,只能使用UIWebview了,这里的加载进度,就比较尴尬了 所以我们 ...
- iOS WKWebView 加载进度条、导航栏返回&关闭 (Swift 4)
导航: 1.加载进度条 2.导航栏增加返回.关闭按钮 加载进度条 效果图 代码如下: self.progressView.trackTintColor = UIColor.white self.pro ...
- 一个KVO 实现WKWebView加载进度条的例子 (注意最后移除观察者)
// // OpenWebViewController.m // Treasure // // Created by 蓝蓝色信子 on 16/7/29. // Copyright © 2016年 GY ...
- css3 linear-gradient实现页面加载进度条效果
最终效果图: html结构: <div> <p class="p1"> <span></span> < ...
- ajax页面加载进度条插件
下面两个都是youtube视频的加载进度条效果的ajax插件 一.官网:http://ricostacruz.com/nprogress/官网 github:https://github.com/rs ...
- pace.js – 加载进度条插件
这儿只是简单介绍一下这个插件pace.js. 在页面中引入Pace.js,页面就会自动监测你的请求(包括Ajax请求),在事件循环滞后,会在页面记录加载的状态以及进度情况.此插件的兼容性很好,可以兼容 ...
- 仿UC浏览器图片加载进度条
前几天用UC浏览器看新闻(无意中给UC打了广告),看到它的图片加载进度条,正好最近有时间,所以就自己写了一个. 效果图如下 进度条的底色和填充颜色都可以调整. 首先中间的笑脸作为一个整体,其实现代码如 ...
- 【Web前沿技术】纯 CSS3 打造的10个精美加载进度条动画
之前向大家介绍8款优秀的 jQuery 加载动画和进度条插件,今天这篇文章向大家推荐10个纯 CSS3 代码实现精美加载进度条动画效果的方案.加载动画和进度条在网站和 Web 应用中的使用非常流行,特 ...
- jQuery模拟页面加载进度条
因为我们无法通过任何方法获取整个页面的大小和当前加载了多少,所以想制作一个加载进度条的唯一办法就是模拟.那要怎么模拟呢? 我们知道,页面是从上往下执行的,也就是说我们可以大致估算出在页面的某个位置加载 ...
随机推荐
- Orchard Core一分钟搭建ASP.NET Core CMS
Orchard Core 是Orchard CMS的ASP.NET Core版本. Orchard Core是全新一代的ASP.NET Core CMS. 官方文档介绍:http://orchardc ...
- (二)部署solr7.1.0到tomcat
solr7.1.0部署到tomcat8 官方表示solr5之后的版本不再提供对第三方容器的支持(不提供war包了). "旧式"solr.xml格式不再支持,核心必须使用core.p ...
- Windows下安装BeautifulSoup
python版本为2.7 1.去官网下载BeautifulSoup4 Beautiful Soup 4.3.2 2.解压文件 将下载得到的压缩包解压到任意文件夹,路径不含中文 3.打开cmd命令提示符 ...
- TP框架Ajax如何使用
ThinkPHP可以很好的支持AJAX请求,系统的\Think\Controller类提供了ajaxReturn方法用于AJAX调用后返回数据给客户端.并且支持JSON.JSONP.XML和EVAL四 ...
- 【Win 10 应用开发】在后台播放视频
从 1607 (14393)版本开始,MediaPlayer 类就可以在前台与后台之间无缝播放,你不必再考虑前台与后之间的通信,所以从 14393 开始,你就不需要再用 BackgroundMedia ...
- MySQL .msi 安装失败改用.zip安装步骤
一开始官网下载.msi安装包,安装到配置server时无法启动,长时间卡在这里,无法继续下去.上网看了一下解决办法,发现用.zip安装包进行安装比较简单可靠. 一.利用.msi安装包安装失败后的处理 ...
- 一个非常好用的图片切割工具(c# winform开发) 附源码
本人业余时间开发了一个图片切割工具,非常好用,也很灵活! 特别对大型图片切割,更能体现出该软件的优势! 开发工具为winform,源码下载地址:http://download.csdn.net/dow ...
- vue2.x利用脚手架快速构建项目并引入bootstrap、jquery
要使用vue-cli脚手架搭建项目,首先需要安装node.js Node.js官网:https://nodejs.org/en/download/ 选择你对应的系统即可下载,下载完成后傻瓜式安装即可. ...
- leetcode算法题3:分组,让每个组的最小者,相加之后和最大。想知道桶排序是怎么样的吗?
/* Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...
- 为什么win记事本编辑的shell在linux中运行会报错
结论:win记事本使用的格式有别于linux,二者不可混用.linux使用一个叫vi的编辑器. 解决办法:使用vi命令建立文件,在其中敲shell.命令:vi > filename (敲完 ...