Skip to content

Commit

Permalink
Log a warning if there was an error while ObjC was retrieving message…
Browse files Browse the repository at this point in the history
…s 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
  • Loading branch information
marcuswestin committed Jan 29, 2016
1 parent 9e7277d commit 7141a41
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 3 additions & 0 deletions WebViewJavascriptBridge/WKWebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ - (void) _setupInstance:(WKWebView*)webView {

- (void)WKFlushMessageQueue {
[_webView evaluateJavaScript:[_base webViewJavascriptFetchQueyCommand] completionHandler:^(NSString* result, NSError* error) {
if (error != nil) {
NSLog(@"WebViewJavascriptBridge: WARNING: Error when trying to fetch data from WKWebView: %@", error);
}
[_base flushMessageQueue:result];
}];
}
Expand Down
7 changes: 4 additions & 3 deletions WebViewJavascriptBridge/WebViewJavascriptBridgeBase.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ - (void)sendData:(id)data responseCallback:(WVJBResponseCallback)responseCallbac
}

- (void)flushMessageQueue:(NSString *)messageQueueString{
id messages = [self _deserializeMessageJSON:messageQueueString];
if (![messages isKindOfClass:[NSArray class]]) {
NSLog(@"WebViewJavascriptBridge: WARNING: Invalid %@ received: %@", [messages class], messages);
if (messageQueueString == nil || messageQueueString.length == 0) {
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.");
return;
}

id messages = [self _deserializeMessageJSON:messageQueueString];
for (WVJBMessage* message in messages) {
if (![message isKindOfClass:[WVJBMessage class]]) {
NSLog(@"WebViewJavascriptBridge: WARNING: Invalid %@ received: %@", [message class], message);
Expand Down

0 comments on commit 7141a41

Please sign in to comment.