首页 技术 正文
技术 2022年11月7日
0 收藏 935 点赞 990 浏览 6620 个字

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/拖到自己的

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,036
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,522
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,370
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,151
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,784
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,866