Skip to content

Commit 7141a41

Browse files
committed
Log a warning if there was an error while ObjC was retrieving messages from the javascript, instead of throwing an exception while attempting to decode the message as JSON.
This can occur e.g if the webview loaded a new page immediately after dispatching a message to ObjC, in which case WebViewJavascriptBridge may not exist in the JS by the time ObjC reaches in to fetch the message queue. Fix marcuswestin#159, marcuswestin#183
1 parent 9e7277d commit 7141a41

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

WebViewJavascriptBridge/WKWebViewJavascriptBridge.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ - (void) _setupInstance:(WKWebView*)webView {
8585

8686
- (void)WKFlushMessageQueue {
8787
[_webView evaluateJavaScript:[_base webViewJavascriptFetchQueyCommand] completionHandler:^(NSString* result, NSError* error) {
88+
if (error != nil) {
89+
NSLog(@"WebViewJavascriptBridge: WARNING: Error when trying to fetch data from WKWebView: %@", error);
90+
}
8891
[_base flushMessageQueue:result];
8992
}];
9093
}

WebViewJavascriptBridge/WebViewJavascriptBridgeBase.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ - (void)sendData:(id)data responseCallback:(WVJBResponseCallback)responseCallbac
6161
}
6262

6363
- (void)flushMessageQueue:(NSString *)messageQueueString{
64-
id messages = [self _deserializeMessageJSON:messageQueueString];
65-
if (![messages isKindOfClass:[NSArray class]]) {
66-
NSLog(@"WebViewJavascriptBridge: WARNING: Invalid %@ received: %@", [messages class], messages);
64+
if (messageQueueString == nil || messageQueueString.length == 0) {
65+
NSLog(@"WebViewJavascriptBridge: WARNING: ObjC got nil while fetching the message queue JSON from webview. This can happen if the WebViewJavascriptBridge JS is not currently present in the webview, e.g if the webview just loaded a new page.");
6766
return;
6867
}
68+
69+
id messages = [self _deserializeMessageJSON:messageQueueString];
6970
for (WVJBMessage* message in messages) {
7071
if (![message isKindOfClass:[WVJBMessage class]]) {
7172
NSLog(@"WebViewJavascriptBridge: WARNING: Invalid %@ received: %@", [message class], message);

0 commit comments

Comments
 (0)