Skip to content

Commit c4cb6ab

Browse files
committed
The declared interface for WVJB expected data to be an id. However, the implementation assumed NSDictionary in a bunch of places. Furthermore, messages received from JS by ObjC were previously always forced into an NSDictionary but javascript did not replace null data packets with {}. This patch introduces consistency in all of these cases, assuming data to be id everywhere, and never changing the value of data.
Drop JSONKit. It's 2013!
1 parent fa0ca36 commit c4cb6ab

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

Changelog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
v4.1.0
2+
+ Allow for sending null/nil data packets
3+
+ Drop support for JSONKit
4+
15
v4.0.2
26
+ Fix NSInvalidArgumentException: "attempt to insert nil object" when using shorthand -callHandler:
37
+ Fix sending messages including __WVJB_MESSAGE_SEPERATOR__ string

WebViewJavascriptBridge/WebViewJavascriptBridge.m

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ + (instancetype)bridgeForWebView:(WVJB_WEBVIEW_TYPE*)webView webViewDelegate:(WV
4747
return bridge;
4848
}
4949

50-
- (void)send:(NSDictionary *)data {
50+
- (void)send:(id)data {
5151
[self send:data responseCallback:nil];
5252
}
5353

54-
- (void)send:(NSDictionary *)data responseCallback:(WVJBResponseCallback)responseCallback {
54+
- (void)send:(id)data responseCallback:(WVJBResponseCallback)responseCallback {
5555
[self _sendData:data responseCallback:responseCallback handlerName:nil];
5656
}
5757

@@ -91,10 +91,7 @@ - (void)dealloc {
9191
_messageHandler = nil;
9292
}
9393

94-
- (void)_sendData:(NSDictionary *)data responseCallback:(WVJBResponseCallback)responseCallback handlerName:(NSString*)handlerName {
95-
if (!data) {
96-
data = (NSDictionary *)[NSNull null];
97-
}
94+
- (void)_sendData:(id)data responseCallback:(WVJBResponseCallback)responseCallback handlerName:(NSString*)handlerName {
9895
NSMutableDictionary* message = [NSMutableDictionary dictionary];
9996

10097
if (data) {
@@ -188,8 +185,7 @@ - (void)_flushMessageQueue {
188185
}
189186

190187
@try {
191-
NSDictionary* data = message[@"data"];
192-
if (!data || ((id)data) == [NSNull null]) { data = [NSDictionary dictionary]; }
188+
id data = message[@"data"];
193189
handler(data, responseCallback);
194190
}
195191
@catch (NSException *exception) {
@@ -199,20 +195,12 @@ - (void)_flushMessageQueue {
199195
}
200196
}
201197

202-
- (NSString *)_serializeMessage:(NSDictionary *)message {
203-
#if defined _JSONKIT_H_
204-
return [message JSONString];
205-
#else
198+
- (NSString *)_serializeMessage:(id)message {
206199
return [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:message options:0 error:nil] encoding:NSUTF8StringEncoding];
207-
#endif
208200
}
209201

210202
- (NSDictionary *)_deserializeMessageJSON:(NSString *)messageJSON {
211-
#if defined _JSONKIT_H_
212-
return [messageJSON objectFromJSONString];
213-
#else
214-
return [NSJSONSerialization JSONObjectWithData:[messageJSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
215-
#endif
203+
return [NSJSONSerialization JSONObjectWithData:[messageJSON dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
216204
}
217205

218206
- (void)_log:(NSString *)action json:(id)json {

0 commit comments

Comments
 (0)