Skip to content

Commit 61b8536

Browse files
author
Peyton Randolph
committed
track number of resources left to load, fixing race condition
1 parent 89d9697 commit 61b8536

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

WebViewJavascriptBridge_iOS/WebViewJavascriptBridge_iOS.m

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#import "WebViewJavascriptBridge_iOS.h"
22

3+
@interface WebViewJavascriptBridge ()
4+
5+
@property (nonatomic, assign) NSUInteger numRequestsLoading;
6+
7+
@end
8+
39
@implementation WebViewJavascriptBridge
410

511
#pragma mark UIWebViewDelegate
@@ -9,7 +15,7 @@ + (id)bridgeForWebView:(UIWebView *)webView handler:(WVJBHandler)handler {
915
}
1016

1117
+ (id)bridgeForWebView:(UIWebView *)webView webViewDelegate:(id<UIWebViewDelegate>)webViewDelegate handler:(WVJBHandler)messageHandler {
12-
WebViewJavascriptBridge* bridge = [[WebViewJavascriptBridge alloc] init];
18+
WebViewJavascriptBridge* bridge = [[[self class] alloc] init];
1319
bridge.messageHandler = messageHandler;
1420
bridge.webView = webView;
1521
bridge.webViewDelegate = webViewDelegate;
@@ -18,13 +24,16 @@ + (id)bridgeForWebView:(UIWebView *)webView webViewDelegate:(id<UIWebViewDelegat
1824

1925
[webView setDelegate:bridge];
2026

27+
bridge.numRequestsLoading = 0;
28+
2129
return bridge;
2230
}
2331

2432
- (void)webViewDidFinishLoad:(UIWebView *)webView {
2533
if (webView != self.webView) { return; }
34+
self.numRequestsLoading--;
2635

27-
if (![[self.webView stringByEvaluatingJavaScriptFromString:@"typeof WebViewJavascriptBridge == 'object'"] isEqualToString:@"true"]) {
36+
if (self.numRequestsLoading == 0 && ![[self.webView stringByEvaluatingJavaScriptFromString:@"typeof WebViewJavascriptBridge == 'object'"] isEqualToString:@"true"]) {
2837
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"WebViewJavascriptBridge.js" ofType:@"txt"];
2938
NSString *js = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
3039
[self.webView stringByEvaluatingJavaScriptFromString:js];
@@ -44,6 +53,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView {
4453

4554
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
4655
if (webView != self.webView) { return; }
56+
self.numRequestsLoading--;
4757
if (self.webViewDelegate && [self.webViewDelegate respondsToSelector:@selector(webView:didFailLoadWithError:)]) {
4858
[self.webViewDelegate webView:self.webView didFailLoadWithError:error];
4959
}
@@ -68,6 +78,7 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
6878

6979
- (void)webViewDidStartLoad:(UIWebView *)webView {
7080
if (webView != self.webView) { return; }
81+
self.numRequestsLoading++;
7182
if (self.webViewDelegate && [self.webViewDelegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
7283
[self.webViewDelegate webViewDidStartLoad:webView];
7384
}

0 commit comments

Comments
 (0)