1
1
#import " WebViewJavascriptBridge_iOS.h"
2
2
3
+ @interface WebViewJavascriptBridge ()
4
+
5
+ @property (nonatomic , assign ) NSUInteger numRequestsLoading;
6
+
7
+ @end
8
+
3
9
@implementation WebViewJavascriptBridge
4
10
5
11
#pragma mark UIWebViewDelegate
@@ -9,7 +15,7 @@ + (id)bridgeForWebView:(UIWebView *)webView handler:(WVJBHandler)handler {
9
15
}
10
16
11
17
+ (id )bridgeForWebView : (UIWebView *)webView webViewDelegate : (id <UIWebViewDelegate>)webViewDelegate handler : (WVJBHandler)messageHandler {
12
- WebViewJavascriptBridge* bridge = [[WebViewJavascriptBridge alloc ] init ];
18
+ WebViewJavascriptBridge* bridge = [[[ self class ] alloc ] init ];
13
19
bridge.messageHandler = messageHandler;
14
20
bridge.webView = webView;
15
21
bridge.webViewDelegate = webViewDelegate;
@@ -18,13 +24,16 @@ + (id)bridgeForWebView:(UIWebView *)webView webViewDelegate:(id<UIWebViewDelegat
18
24
19
25
[webView setDelegate: bridge];
20
26
27
+ bridge.numRequestsLoading = 0 ;
28
+
21
29
return bridge;
22
30
}
23
31
24
32
- (void )webViewDidFinishLoad : (UIWebView *)webView {
25
33
if (webView != self.webView ) { return ; }
34
+ self.numRequestsLoading --;
26
35
27
- if (![[self .webView stringByEvaluatingJavaScriptFromString: @" typeof WebViewJavascriptBridge == 'object'" ] isEqualToString: @" true" ]) {
36
+ if (self. numRequestsLoading == 0 && ![[self .webView stringByEvaluatingJavaScriptFromString: @" typeof WebViewJavascriptBridge == 'object'" ] isEqualToString: @" true" ]) {
28
37
NSString *filePath = [[NSBundle mainBundle ] pathForResource: @" WebViewJavascriptBridge.js" ofType: @" txt" ];
29
38
NSString *js = [NSString stringWithContentsOfFile: filePath encoding: NSUTF8StringEncoding error: nil ];
30
39
[self .webView stringByEvaluatingJavaScriptFromString: js];
@@ -44,6 +53,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView {
44
53
45
54
- (void )webView : (UIWebView *)webView didFailLoadWithError : (NSError *)error {
46
55
if (webView != self.webView ) { return ; }
56
+ self.numRequestsLoading --;
47
57
if (self.webViewDelegate && [self .webViewDelegate respondsToSelector: @selector (webView:didFailLoadWithError: )]) {
48
58
[self .webViewDelegate webView: self .webView didFailLoadWithError: error];
49
59
}
@@ -68,6 +78,7 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
68
78
69
79
- (void )webViewDidStartLoad : (UIWebView *)webView {
70
80
if (webView != self.webView ) { return ; }
81
+ self.numRequestsLoading ++;
71
82
if (self.webViewDelegate && [self .webViewDelegate respondsToSelector: @selector (webViewDidStartLoad: )]) {
72
83
[self .webViewDelegate webViewDidStartLoad: webView];
73
84
}
0 commit comments