@@ -92,6 +92,9 @@ - (void)dealloc {
9292}
9393
9494- (void )_sendData : (NSDictionary *)data responseCallback : (WVJBResponseCallback)responseCallback handlerName : (NSString *)handlerName {
95+ if (!data) {
96+ data = (NSDictionary *)[NSNull null ];
97+ }
9598 NSMutableDictionary * message = [NSMutableDictionary dictionaryWithObject: data forKey: @" data" ];
9699
97100 if (responseCallback) {
@@ -138,12 +141,18 @@ - (void)_dispatchMessage:(NSDictionary *)message {
138141- (void )_flushMessageQueue {
139142 NSString *messageQueueString = [_webView stringByEvaluatingJavaScriptFromString: @" WebViewJavascriptBridge._fetchQueue();" ];
140143
141- NSArray * messages = [messageQueueString componentsSeparatedByString: kMessageSeparator ];
142- for (NSString *messageJSON in messages) {
143- [self _log: @" receivd" json: messageJSON];
144-
145- NSDictionary * message = [self _deserializeMessageJSON: messageJSON];
146-
144+ id messages = [self _deserializeMessageJSON: messageQueueString];
145+ if (![messages isKindOfClass: [NSArray class ]]) {
146+ NSLog (@" WebViewJavascriptBridge: WARNING: Invalid %@ received: %@ " , [messages class ], messages);
147+ return ;
148+ }
149+ for (NSDictionary *message in messages) {
150+ if (![message isKindOfClass: [NSDictionary class ]]) {
151+ NSLog (@" WebViewJavascriptBridge: WARNING: Invalid %@ received: %@ " , [message class ], message);
152+ continue ;
153+ }
154+ [self _log: @" receivd" json: message];
155+
147156 NSString * responseId = message[@" responseId" ];
148157 if (responseId) {
149158 WVJBResponseCallback responseCallback = _responseCallbacks[responseId];
@@ -202,10 +211,13 @@ - (NSDictionary *)_deserializeMessageJSON:(NSString *)messageJSON {
202211#endif
203212}
204213
205- - (void )_log : (NSString *)action json : (NSString * )json {
214+ - (void )_log : (NSString *)action json : (id )json {
206215 if (!logging) { return ; }
207- if (json.length > 500 ) {
208- NSLog (@" WVJB %@ : %@ " , action, [[json substringToIndex: 500 ] stringByAppendingString: @" [...]" ]);
216+ if (![json isKindOfClass: [NSString class ]]) {
217+ json = [self _serializeMessage: json];
218+ }
219+ if ([json length ] > 500 ) {
220+ NSLog (@" WVJB %@ : %@ [...]" , action, [json substringToIndex: 500 ]);
209221 } else {
210222 NSLog (@" WVJB %@ : %@ " , action, json);
211223 }
0 commit comments