|
| 1 | +// |
| 2 | +// NJKWebViewProgress.m |
| 3 | +// |
| 4 | +// Created by Satoshi Aasano on 4/20/13. |
| 5 | +// Copyright (c) 2013 Satoshi Asano. All rights reserved. |
| 6 | +// |
| 7 | + |
| 8 | +#import "NJKWebViewProgress.h" |
| 9 | + |
| 10 | +NSString *completeRPCURLPath = @"/njkwebviewprogressproxy/complete"; |
| 11 | + |
| 12 | +const float NJKInitialProgressValue = 0.1f; |
| 13 | +const float NJKInteractiveProgressValue = 0.5f; |
| 14 | +const float NJKFinalProgressValue = 0.9f; |
| 15 | + |
| 16 | +@implementation NJKWebViewProgress |
| 17 | +{ |
| 18 | + NSUInteger _loadingCount; |
| 19 | + NSUInteger _maxLoadCount; |
| 20 | + NSURL *_currentURL; |
| 21 | + BOOL _interactive; |
| 22 | +} |
| 23 | + |
| 24 | +- (id)init |
| 25 | +{ |
| 26 | + self = [super init]; |
| 27 | + if (self) { |
| 28 | + _maxLoadCount = _loadingCount = 0; |
| 29 | + _interactive = NO; |
| 30 | + } |
| 31 | + return self; |
| 32 | +} |
| 33 | + |
| 34 | +- (void)startProgress |
| 35 | +{ |
| 36 | + if (_progress < NJKInitialProgressValue) { |
| 37 | + [self setProgress:NJKInitialProgressValue]; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +- (void)incrementProgress |
| 42 | +{ |
| 43 | + float progress = self.progress; |
| 44 | + float maxProgress = _interactive ? NJKFinalProgressValue : NJKInteractiveProgressValue; |
| 45 | + float remainPercent = (float)_loadingCount / (float)_maxLoadCount; |
| 46 | + float increment = (maxProgress - progress) * remainPercent; |
| 47 | + progress += increment; |
| 48 | + progress = fmin(progress, maxProgress); |
| 49 | + [self setProgress:progress]; |
| 50 | +} |
| 51 | + |
| 52 | +- (void)completeProgress |
| 53 | +{ |
| 54 | + [self setProgress:1.0]; |
| 55 | +} |
| 56 | + |
| 57 | +- (void)setProgress:(float)progress |
| 58 | +{ |
| 59 | + // progress should be incremental only |
| 60 | + if (progress > _progress || progress == 0) { |
| 61 | + _progress = progress; |
| 62 | + if ([_progressDelegate respondsToSelector:@selector(webViewProgress:updateProgress:)]) { |
| 63 | + [_progressDelegate webViewProgress:self updateProgress:progress]; |
| 64 | + } |
| 65 | + if (_progressBlock) { |
| 66 | + _progressBlock(progress); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +- (void)reset |
| 72 | +{ |
| 73 | + _maxLoadCount = _loadingCount = 0; |
| 74 | + _interactive = NO; |
| 75 | + [self setProgress:0.0]; |
| 76 | +} |
| 77 | + |
| 78 | +#pragma mark - |
| 79 | +#pragma mark UIWebViewDelegate |
| 80 | + |
| 81 | +- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType |
| 82 | +{ |
| 83 | + if ([request.URL.path isEqualToString:completeRPCURLPath]) { |
| 84 | + [self completeProgress]; |
| 85 | + return NO; |
| 86 | + } |
| 87 | + |
| 88 | + BOOL ret = YES; |
| 89 | + if ([_webViewProxyDelegate respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)]) { |
| 90 | + ret = [_webViewProxyDelegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType]; |
| 91 | + } |
| 92 | + |
| 93 | + BOOL isFragmentJump = NO; |
| 94 | + if (request.URL.fragment) { |
| 95 | + NSString *nonFragmentURL = [request.URL.absoluteString stringByReplacingOccurrencesOfString:[@"#" stringByAppendingString:request.URL.fragment] withString:@""]; |
| 96 | + isFragmentJump = [nonFragmentURL isEqualToString:webView.request.URL.absoluteString]; |
| 97 | + } |
| 98 | + |
| 99 | + BOOL isTopLevelNavigation = [request.mainDocumentURL isEqual:request.URL]; |
| 100 | + |
| 101 | + BOOL isHTTPOrLocalFile = [request.URL.scheme isEqualToString:@"http"] || [request.URL.scheme isEqualToString:@"https"] || [request.URL.scheme isEqualToString:@"file"]; |
| 102 | + if (ret && !isFragmentJump && isHTTPOrLocalFile && isTopLevelNavigation) { |
| 103 | + _currentURL = request.URL; |
| 104 | + [self reset]; |
| 105 | + } |
| 106 | + return ret; |
| 107 | +} |
| 108 | + |
| 109 | +- (void)webViewDidStartLoad:(UIWebView *)webView |
| 110 | +{ |
| 111 | + if ([_webViewProxyDelegate respondsToSelector:@selector(webViewDidStartLoad:)]) { |
| 112 | + [_webViewProxyDelegate webViewDidStartLoad:webView]; |
| 113 | + } |
| 114 | + |
| 115 | + _loadingCount++; |
| 116 | + _maxLoadCount = fmax(_maxLoadCount, _loadingCount); |
| 117 | + |
| 118 | + [self startProgress]; |
| 119 | +} |
| 120 | + |
| 121 | +- (void)webViewDidFinishLoad:(UIWebView *)webView |
| 122 | +{ |
| 123 | + if ([_webViewProxyDelegate respondsToSelector:@selector(webViewDidFinishLoad:)]) { |
| 124 | + [_webViewProxyDelegate webViewDidFinishLoad:webView]; |
| 125 | + } |
| 126 | + |
| 127 | + _loadingCount--; |
| 128 | + [self incrementProgress]; |
| 129 | + |
| 130 | + NSString *readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"]; |
| 131 | + |
| 132 | + BOOL interactive = [readyState isEqualToString:@"interactive"]; |
| 133 | + if (interactive) { |
| 134 | + _interactive = YES; |
| 135 | + 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]; |
| 136 | + [webView stringByEvaluatingJavaScriptFromString:waitForCompleteJS]; |
| 137 | + } |
| 138 | + |
| 139 | + BOOL isNotRedirect = _currentURL && [_currentURL isEqual:webView.request.mainDocumentURL]; |
| 140 | + BOOL complete = [readyState isEqualToString:@"complete"]; |
| 141 | + if (complete && isNotRedirect) { |
| 142 | + [self completeProgress]; |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error |
| 147 | +{ |
| 148 | + if ([_webViewProxyDelegate respondsToSelector:@selector(webView:didFailLoadWithError:)]) { |
| 149 | + [_webViewProxyDelegate webView:webView didFailLoadWithError:error]; |
| 150 | + } |
| 151 | + |
| 152 | + _loadingCount--; |
| 153 | + [self incrementProgress]; |
| 154 | + |
| 155 | + NSString *readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"]; |
| 156 | + |
| 157 | + BOOL interactive = [readyState isEqualToString:@"interactive"]; |
| 158 | + if (interactive) { |
| 159 | + _interactive = YES; |
| 160 | + 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]; |
| 161 | + [webView stringByEvaluatingJavaScriptFromString:waitForCompleteJS]; |
| 162 | + } |
| 163 | + |
| 164 | + BOOL isNotRedirect = _currentURL && [_currentURL isEqual:webView.request.mainDocumentURL]; |
| 165 | + BOOL complete = [readyState isEqualToString:@"complete"]; |
| 166 | + if ((complete && isNotRedirect) || error) { |
| 167 | + [self completeProgress]; |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +#pragma mark - |
| 172 | +#pragma mark Method Forwarding |
| 173 | +// for future UIWebViewDelegate impl |
| 174 | + |
| 175 | +- (BOOL)respondsToSelector:(SEL)aSelector |
| 176 | +{ |
| 177 | + if ( [super respondsToSelector:aSelector] ) |
| 178 | + return YES; |
| 179 | + |
| 180 | + if ([self.webViewProxyDelegate respondsToSelector:aSelector]) |
| 181 | + return YES; |
| 182 | + |
| 183 | + return NO; |
| 184 | +} |
| 185 | + |
| 186 | +- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector |
| 187 | +{ |
| 188 | + NSMethodSignature *signature = [super methodSignatureForSelector:selector]; |
| 189 | + if(!signature) { |
| 190 | + if([_webViewProxyDelegate respondsToSelector:selector]) { |
| 191 | + return [(NSObject *)_webViewProxyDelegate methodSignatureForSelector:selector]; |
| 192 | + } |
| 193 | + } |
| 194 | + return signature; |
| 195 | +} |
| 196 | + |
| 197 | +- (void)forwardInvocation:(NSInvocation*)invocation |
| 198 | +{ |
| 199 | + if ([_webViewProxyDelegate respondsToSelector:[invocation selector]]) { |
| 200 | + [invocation invokeWithTarget:_webViewProxyDelegate]; |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +@end |
0 commit comments