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模拟页面加载进度条
因为我们无法通过任何方法获取整个页面的大小和当前加载了多少,所以想制作一个加载进度条的唯一办法就是模拟.那要怎么模拟呢? 我们知道,页面是从上往下执行的,也就是说我们可以大致估算出在页面的某个位置加载 ...
随机推荐
- django实现分片上传文件
目标:利用django实现上传文件功能 1,先设置路由系统 urls.py from django.conf.urls import url,include from django.contrib i ...
- memcache的使用、版本使用和相关配置
首先准备memcached和php_memcache.dll文件.下载网址:链接:http://pan.baidu.com/s/1c1WODji 密码:yzor 将下载好的memcached.exe放 ...
- 《Linux命令行与shell脚本编程大全》第二十一章 sed进阶
本章介绍一些sed编辑器提供的高级特性. 21.1 多行命令 按照之前的知识,所有的sed编辑器命令都是针对单行数据执行操作的. 在sed编辑器读取数据流时,它会基于换行符的位置将数据分成行,一次处理 ...
- Carbondata源码系列(二)文件格式详解
在上一章当中,写了文件的生成过程.这一章主要讲解文件格式(V3版本)的具体细节. 1.字典文件格式详解 字典文件的作用是在存储的时候将字符串等类型转换为int类型,好处主要有两点: 1.减少存储占用空 ...
- Shell编程进阶篇(完结)
1.1 for循环语句 在计算机科学中,for循环(英语:for loop)是一种编程语言的迭代陈述,能够让程式码反复的执行. 它跟其他的循环,如while循环,最大的不同,是它拥有一个循环计数器,或 ...
- C#_表达式目录树的应用
使用表达式目录树实现两个不同类型的属性赋值: public class People { public int Age { get; set; } public string Name { get; ...
- C# Value type vs Reference type
[MY NOTE] [转载请注明出处] Reference Source: http://www.albahari.com/valuevsreftypes.aspx http://www.c-sh ...
- django.db.utils.OperationalError: 1050解决方案
manage.py migrate时进行同步数据库时出现问题;django.db.utils.OperationalError: (1050, "Table '表名' already exi ...
- 《iOS Human Interface Guidelines》——Multitasking
多任务处理 多任务处理让人们在屏幕上(以及合适的iPad模式)查看多个app,而且在近期使用的app中高速地切换. 在iOS 9中.人们能够使用多任务处理UI(例如以下所看到的)来选择一个近期使用的a ...
- android 读取系统文件 wpa_supplicant
1,须要权限 <uses-permission android:name="android.permission.ACCESS_SUPERUSER" /> 2,下载 R ...