Permalink
Browse files

Add [WebViewJavascriptBridge enableLogging] for easier debugging

  • Loading branch information...
1 parent 01719a5 commit 57d62993a228a3382d5483f794f5a1c9789c2916 @marcuswestin committed Oct 7, 2012
@@ -10,6 +10,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
UIWebView* webView = [[UIWebView alloc] initWithFrame:self.window.bounds];
[self.window addSubview:webView];
+ [WebViewJavascriptBridge enableLogging];
+
_bridge = [WebViewJavascriptBridge bridgeForWebView:webView handler:^(id data, WVJBResponse *response) {
NSLog(@"ObjC received message from JS: %@", data);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ObjC got message from Javascript:" message:data delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
@@ -7,6 +7,7 @@ typedef void (^WVJBHandler)(id data, WVJBResponse* response);
@interface WebViewJavascriptBridge : NSObject <UIWebViewDelegate>
+ (id)bridgeForWebView:(UIWebView*)webView handler:(WVJBHandler)handler;
+ (id)bridgeForWebView:(UIWebView*)webView webViewDelegate:(id <UIWebViewDelegate>)webViewDelegate handler:(WVJBHandler)handler;
++ (void)enableLogging;
- (void)send:(id)message;
- (void)send:(id)message responseCallback:(WVJBResponseCallback)responseCallback;
- (void)registerHandler:(NSString*)handlerName handler:(WVJBHandler)handler;
@@ -46,6 +46,9 @@ + (id)bridgeForWebView:(UIWebView *)webView webViewDelegate:(id<UIWebViewDelegat
return bridge;
}
+static bool logging = false;
++ (void)enableLogging { logging = true; }
+
- (void)send:(NSDictionary *)data {
[self send:data responseCallback:nil];
}
@@ -95,6 +98,7 @@ - (void)_queueMessage:(NSDictionary *)message {
- (void)_dispatchMessage:(NSDictionary *)message {
NSString *messageJSON = [self _serializeMessage:message];
+ if (logging) { NSLog(@"WVJB: send %@", messageJSON); }
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"];
@@ -112,10 +116,12 @@ - (void)_flushMessageQueue {
NSString* responseId = [message objectForKey:@"responseId"];
if (responseId) {
+ if (logging) { NSLog(@"WVJB response: %@", messageJSON); }
WVJBResponseCallback responseCallback = [_responseCallbacks objectForKey:responseId];
responseCallback([message objectForKey:@"error"], [message objectForKey:@"responseData"]);
[_responseCallbacks removeObjectForKey:responseId];
} else {
+ if (logging) { NSLog(@"WVJB message: %@", messageJSON); }
WVJBResponse* response = nil;
if ([message objectForKey:@"callbackId"]) {
response = [[WVJBResponse alloc] initWithCallbackId:[message objectForKey:@"callbackId"] bridge:self];

0 comments on commit 57d6299

Please sign in to comment.