NJKWebViewProgress地址:https://github.com/ninjinkun/NJKWebViewProgress
导入头文件
#import "NJKWebViewProgressView.h"
#import "NJKWebViewProgress.h"
遵守协议
<UIWebViewDelegate, NJKWebViewProgressDelegate>
实现代码
@implementation ViewController
{
IBOutlet __weak UIWebView *_webView;
NJKWebViewProgressView *_webViewProgressView;
NJKWebViewProgress *_webViewProgress;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_webViewProgress = [[NJKWebViewProgress alloc] init];
_webView.delegate = _webViewProgress;
_webViewProgress.webViewProxyDelegate = self;
_webViewProgress.progressDelegate = self;
CGRect navBounds = self.navigationController.navigationBar.bounds;
CGRect barFrame = CGRectMake(0,
navBounds.size.height - 2,
navBounds.size.width,
2);
_webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
_webViewProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[_webViewProgressView setProgress:0 animated:YES];
[self loadBaidu];
[self.navigationController.navigationBar addSubview:_webViewProgressView];
}
-(void)loadBidu
{
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.baidu.com/"]];
[_webView loadRequest:req];
}
#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
[_webViewProgressView setProgress:progress animated:YES];
self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
网友评论
// 若无导航栏,64.0 改为 0.0
self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0.0, 64.0, self.view.frame.size.width, 1)];
[self.view addSubview:self.progressView];
第二步:添加监听事件
- (void)viewDidLoad{
[self.webView addObserver:self
forKeyPath:@"loading"
options:NSKeyValueObservingOptionNew
context:nil];
}
第三步:不用时隐藏
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"loading"]) {
self.progressView.hidden = NO;
}
if (!self.webView.loading) {
[UIView animateWithDuration:0.5
animations:^{
self.progressView.hidden = YES;
} completion:nil];
}
}
NSString *waitForCompleteJS = [NSString stringWithFormat:@"window.addEventListener('load',function() { var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = '%@://%@%@'; document.body.appendChild(iframe); }, false);", webView.request.mainDocumentURL.scheme, webView.request.mainDocumentURL.host, completeRPCURLPath];
[webView stringByEvaluatingJavaScriptFromString:waitForCompleteJS];
但是在shouldStartLoadWithRequest方法中,他又做了:
if ([request.URL.path isEqualToString:completeRPCURLPath]) {
[self completeProgress];
return NO;
}
这样一个操作。很显然上面的js之行,这个shouldStartLoadWithRequest 就会return NO。
这样写的目的是什么?还有completeRPCURLPath 这个path的意义是什么?
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if ([_webViewProxyDelegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
[_webViewProxyDelegate webViewDidFinishLoad:webView];
}
_loadingCount--;
[self incrementProgress];
NSString *readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"];
BOOL interactive = [readyState isEqualToString:@"interactive"];
if (interactive) {
_interactive = YES;
NSString *waitForCompleteJS = [NSString stringWithFormat:@"window.addEventListener('load',function() { var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = '%@://%@%@'; document.body.appendChild(iframe); }, false);", webView.request.mainDocumentURL.scheme, webView.request.mainDocumentURL.host, completeRPCURLPath];
[webView stringByEvaluatingJavaScriptFromString:waitForCompleteJS];
}
BOOL isNotRedirect = _currentURL && [_currentURL isEqual:webView.request.mainDocumentURL];
BOOL complete = [readyState isEqualToString:@"complete"];
if (complete && isNotRedirect) {
[self completeProgress];
}
}
这个里边执行了:waitForCompleteJS 这个js。那么shouldStartLoadWithRequest这个方法里办就会执行 if ([request.URL.path isEqualToString:completeRPCURLPath]) {
[self completeProgress];
return NO;
}
这个逻辑。但是不知道他的功能
{
[self.progressView setProgress:progress animated:YES];
}
我在这个方法中,progress 值有时候是0.5,但是网页明明加载完了,怎么破
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[_webViewProgressView setProgress:1 animated:YES];
}
这个就是进度条的颜色,上面的是我改为红色的颜色。