77
88#import " NJKWebViewProgress.h"
99
10- NSString *completeRPCURL = @" webviewprogressproxy:// /complete" ;
10+ NSString *completeRPCURLPath = @" /njkwebviewprogressproxy /complete" ;
1111
1212const float NJKInitialProgressValue = 0 .1f ;
1313const float NJKInteractiveProgressValue = 0 .5f ;
1414const float NJKFinalProgressValue = 0 .9f ;
1515
16+ @interface NJKWebViewProgress ()
17+ @property (strong , nonatomic ) NSTimer *timer;
18+ @end
19+
1620@implementation NJKWebViewProgress
1721{
1822 NSUInteger _loadingCount;
@@ -35,6 +39,7 @@ - (void)startProgress
3539{
3640 if (_progress < NJKInitialProgressValue) {
3741 [self setProgress: NJKInitialProgressValue];
42+ [self startUpTimer ];
3843 }
3944}
4045
@@ -52,6 +57,7 @@ - (void)incrementProgress
5257- (void )completeProgress
5358{
5459 [self setProgress: 1.0 ];
60+ [self invalidateTimer ];
5561}
5662
5763- (void )setProgress : (float )progress
@@ -75,12 +81,33 @@ - (void)reset
7581 [self setProgress: 0.0 ];
7682}
7783
78- #pragma mark -
84+ #pragma mark timer
85+ - (void )startUpTimer {
86+ NSTimeInterval timeStep = 0.1 ;
87+ self.timer = [NSTimer scheduledTimerWithTimeInterval: timeStep
88+ target: self
89+ selector: @selector (timerStep: )
90+ userInfo: nil
91+ repeats: YES ];
92+ }
93+
94+ - (void )invalidateTimer {
95+ [self .timer invalidate ];
96+ self.timer = nil ;
97+ }
98+
99+ - (void )timerStep : (NSTimer *)timer {
100+ static float progressStep = 0.02 ;
101+ if (_progress + progressStep <= NJKFinalProgressValue) {
102+ self.progress += progressStep;
103+ }
104+ }
105+
79106#pragma mark UIWebViewDelegate
80107
81108- (BOOL )webView : (UIWebView *)webView shouldStartLoadWithRequest : (NSURLRequest *)request navigationType : (UIWebViewNavigationType)navigationType
82109{
83- if ([request.URL .absoluteString isEqualToString: completeRPCURL ]) {
110+ if ([request.URL .path isEqualToString: completeRPCURLPath ]) {
84111 [self completeProgress ];
85112 return NO ;
86113 }
@@ -98,9 +125,8 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
98125
99126 BOOL isTopLevelNavigation = [request.mainDocumentURL isEqual: request.URL ];
100127
101- BOOL isHTTP = [request.URL .scheme isEqualToString: @" http" ] || [request.URL .scheme isEqualToString: @" https" ];
102- BOOL isFile = [request.URL .scheme isEqualToString: @" file" ];// easeeeeeeeee modify
103- if (ret && !isFragmentJump && (isHTTP || isFile) && isTopLevelNavigation) {
128+ BOOL isHTTPOrLocalFile = [request.URL .scheme isEqualToString: @" http" ] || [request.URL .scheme isEqualToString: @" https" ] || [request.URL .scheme isEqualToString: @" file" ];
129+ if (ret && !isFragmentJump && isHTTPOrLocalFile && isTopLevelNavigation) {
104130 _currentURL = request.URL ;
105131 [self reset ];
106132 }
@@ -133,7 +159,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView
133159 BOOL interactive = [readyState isEqualToString: @" interactive" ];
134160 if (interactive) {
135161 _interactive = YES ;
136- NSString *waitForCompleteJS = [NSString stringWithFormat: @" window.addEventListener('load',function() { var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = '%@ '; document.body.appendChild(iframe); }, false);" , completeRPCURL ];
162+ 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 ];
137163 [webView stringByEvaluatingJavaScriptFromString: waitForCompleteJS];
138164 }
139165
@@ -158,14 +184,13 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
158184 BOOL interactive = [readyState isEqualToString: @" interactive" ];
159185 if (interactive) {
160186 _interactive = YES ;
161- NSString *waitForCompleteJS = [NSString stringWithFormat: @" window.addEventListener('load',function() { var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = '%@ '; document.body.appendChild(iframe); }, false);" , completeRPCURL ];
187+ 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 ];
162188 [webView stringByEvaluatingJavaScriptFromString: waitForCompleteJS];
163189 }
164190
165191 BOOL isNotRedirect = _currentURL && [_currentURL isEqual: webView.request.mainDocumentURL];
166192 BOOL complete = [readyState isEqualToString: @" complete" ];
167- BOOL mainDocumentURLNotExist = !webView.request .mainDocumentURL ;
168- if ((complete && isNotRedirect) || mainDocumentURLNotExist) {
193+ if ((complete && isNotRedirect) || error) {
169194 [self completeProgress ];
170195 }
171196}
0 commit comments