From 4e990fb86358a227d2cbede70e79fa722e3e0378 Mon Sep 17 00:00:00 2001 From: lokimeyburg Date: Sat, 8 Nov 2014 17:46:04 -0800 Subject: [PATCH] Removed compiler warnings --- .../WKWebViewJavascriptBridge.h | 10 +- .../WKWebViewJavascriptBridge.m | 19 ++- .../WebViewJavascriptBridge.h | 8 +- .../WebViewJavascriptBridge.m | 27 ++-- .../WebViewJavascriptBridgeBase.h | 45 ++---- .../WebViewJavascriptBridgeBase.m | 145 ++++++++---------- 6 files changed, 107 insertions(+), 147 deletions(-) diff --git a/WebViewJavascriptBridge/WKWebViewJavascriptBridge.h b/WebViewJavascriptBridge/WKWebViewJavascriptBridge.h index 89c7e0c7..2ed75d24 100644 --- a/WebViewJavascriptBridge/WKWebViewJavascriptBridge.h +++ b/WebViewJavascriptBridge/WKWebViewJavascriptBridge.h @@ -1,8 +1,8 @@ // // WKWebViewJavascriptBridge.h // -// Created by Loki Meyburg on 10/15/14. -// Copyright (c) 2014 Loki Meyburg. All rights reserved. +// Created by @LokiMeyburg on 10/15/14. +// Copyright (c) 2014 @LokiMeyburg. All rights reserved. // #if (__MAC_OS_X_VERSION_MAX_ALLOWED > __MAC_10_9 || __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1) @@ -12,12 +12,10 @@ #if defined(supportsWKWebKit ) #import +#import "WebViewJavascriptBridgeBase.h" #import -typedef void (^WVJBResponseCallback)(id responseData); -typedef void (^WVJBHandler)(id data, WVJBResponseCallback responseCallback); - -@interface WKWebViewJavascriptBridge : NSObject +@interface WKWebViewJavascriptBridge : NSObject + (instancetype)bridgeForWebView:(WKWebView*)webView handler:(WVJBHandler)handler; + (instancetype)bridgeForWebView:(WKWebView*)webView webViewDelegate:(NSObject*)webViewDelegate handler:(WVJBHandler)handler; diff --git a/WebViewJavascriptBridge/WKWebViewJavascriptBridge.m b/WebViewJavascriptBridge/WKWebViewJavascriptBridge.m index f2a7a586..fb3c8270 100644 --- a/WebViewJavascriptBridge/WKWebViewJavascriptBridge.m +++ b/WebViewJavascriptBridge/WKWebViewJavascriptBridge.m @@ -1,13 +1,12 @@ // // WKWebViewJavascriptBridge.m // -// Created by Loki Meyburg on 10/15/14. -// Copyright (c) 2014 Loki Meyburg. All rights reserved. +// Created by @LokiMeyburg on 10/15/14. +// Copyright (c) 2014 @LokiMeyburg. All rights reserved. // #import "WKWebViewJavascriptBridge.h" -#import "WebViewJavascriptBridgeBase.h" #if defined(supportsWKWebKit) @@ -44,7 +43,7 @@ - (void)send:(id)data { } - (void)send:(id)data responseCallback:(WVJBResponseCallback)responseCallback { - [_base _sendData:data responseCallback:responseCallback handlerName:nil]; + [_base sendData:data responseCallback:responseCallback handlerName:nil]; } - (void)callHandler:(NSString *)handlerName { @@ -56,7 +55,7 @@ - (void)callHandler:(NSString *)handlerName data:(id)data { } - (void)callHandler:(NSString *)handlerName data:(id)data responseCallback:(WVJBResponseCallback)responseCallback { - [_base _sendData:data responseCallback:responseCallback handlerName:handlerName]; + [_base sendData:data responseCallback:responseCallback handlerName:handlerName]; } - (void)registerHandler:(NSString *)handlerName handler:(WVJBHandler)handler { @@ -85,14 +84,14 @@ - (void) _setupInstance:(WKWebView*)webView webViewDelegate:(id +#import "WebViewJavascriptBridgeBase.h" #if defined __MAC_OS_X_VERSION_MAX_ALLOWED #import #define WVJB_PLATFORM_OSX #define WVJB_WEBVIEW_TYPE WebView - #define WVJB_WEBVIEW_DELEGATE_TYPE NSObject + #define WVJB_WEBVIEW_DELEGATE_TYPE NSObject #elif defined __IPHONE_OS_VERSION_MAX_ALLOWED #import #define WVJB_PLATFORM_IOS #define WVJB_WEBVIEW_TYPE UIWebView - #define WVJB_WEBVIEW_DELEGATE_TYPE NSObject + #define WVJB_WEBVIEW_DELEGATE_TYPE NSObject #endif -typedef void (^WVJBResponseCallback)(id responseData); -typedef void (^WVJBHandler)(id data, WVJBResponseCallback responseCallback); - @interface WebViewJavascriptBridge : WVJB_WEBVIEW_DELEGATE_TYPE + (instancetype)bridgeForWebView:(WVJB_WEBVIEW_TYPE*)webView handler:(WVJBHandler)handler; diff --git a/WebViewJavascriptBridge/WebViewJavascriptBridge.m b/WebViewJavascriptBridge/WebViewJavascriptBridge.m index 7f5cad1e..2eb09e95 100644 --- a/WebViewJavascriptBridge/WebViewJavascriptBridge.m +++ b/WebViewJavascriptBridge/WebViewJavascriptBridge.m @@ -7,7 +7,6 @@ // #import "WebViewJavascriptBridge.h" -#import "WebViewJavascriptBridgeBase.h" #if __has_feature(objc_arc_weak) #define WVJB_WEAK __weak @@ -15,8 +14,6 @@ #define WVJB_WEAK __unsafe_unretained #endif -typedef NSDictionary WVJBMessage; - @implementation WebViewJavascriptBridge { WVJB_WEAK WVJB_WEBVIEW_TYPE* _webView; WVJB_WEAK id _webViewDelegate; @@ -53,7 +50,7 @@ - (void)send:(id)data { } - (void)send:(id)data responseCallback:(WVJBResponseCallback)responseCallback { - [_base _sendData:data responseCallback:responseCallback handlerName:nil]; + [_base sendData:data responseCallback:responseCallback handlerName:nil]; } - (void)callHandler:(NSString *)handlerName { @@ -65,7 +62,7 @@ - (void)callHandler:(NSString *)handlerName data:(id)data { } - (void)callHandler:(NSString *)handlerName data:(id)data responseCallback:(WVJBResponseCallback)responseCallback { - [_base _sendData:data responseCallback:responseCallback handlerName:handlerName]; + [_base sendData:data responseCallback:responseCallback handlerName:handlerName]; } - (void)registerHandler:(NSString *)handlerName handler:(WVJBHandler)handler { @@ -99,7 +96,7 @@ - (void) _platformSpecificSetup:(WVJB_WEBVIEW_TYPE*)webView webViewDelegate:(WVJ _webView.resourceLoadDelegate = self; _webView.policyDelegate = self; - _base = [[WebViewJavascriptBridgeBase alloc] initWithWebViewType:@"WebView" handler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle]; + _base = [[WebViewJavascriptBridgeBase alloc] initWithHandler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle]; _base.delegate = self; } @@ -114,7 +111,7 @@ - (void)webView:(WebView *)webView didFinishLoadForFrame:(WebFrame *)frame if (webView != _webView) { return; } if (![[webView stringByEvaluatingJavaScriptFromString:[_base webViewJavascriptCheckCommand]] isEqualToString:@"true"]) { - [_base injectJavascriptFile:NO]; + [_base injectJavascriptFile:YES]; } [_base dispatchStartUpMessageQueue]; @@ -137,10 +134,10 @@ - (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary if (webView != _webView) { return; } NSURL *url = [request URL]; - if ([_base correctProcotocolScheme:url]) { - if ([_base correctHost:url]) { + if ([_base isCorrectProcotocolScheme:url]) { + if ([_base isCorrectHost:url]) { NSString *messageQueueString = [self _evaluateJavascript:[_base webViewJavascriptFetchQueyCommand]]; - [_base _flushMessageQueue:messageQueueString]; + [_base flushMessageQueue:messageQueueString]; } else { [_base logUnkownMessage:url]; } @@ -180,7 +177,7 @@ - (void) _platformSpecificSetup:(WVJB_WEBVIEW_TYPE*)webView webViewDelegate:(id< _webView = webView; _webView.delegate = self; _webViewDelegate = webViewDelegate; - _base = [[WebViewJavascriptBridgeBase alloc] initWithWebViewType:@"WebView" handler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle]; + _base = [[WebViewJavascriptBridgeBase alloc] initWithHandler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle]; _base.delegate = self; } @@ -194,7 +191,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView { _numRequestsLoading--; if (_numRequestsLoading == 0 && ![[webView stringByEvaluatingJavaScriptFromString:[_base webViewJavascriptCheckCommand]] isEqualToString:@"true"]) { - [_base injectJavascriptFile:NO]; + [_base injectJavascriptFile:YES]; } [_base dispatchStartUpMessageQueue]; @@ -220,10 +217,10 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *) if (webView != _webView) { return YES; } NSURL *url = [request URL]; __strong WVJB_WEBVIEW_DELEGATE_TYPE* strongDelegate = _webViewDelegate; - if ([_base correctProcotocolScheme:url]) { - if ([_base correctHost:url]) { + if ([_base isCorrectProcotocolScheme:url]) { + if ([_base isCorrectHost:url]) { NSString *messageQueueString = [self _evaluateJavascript:[_base webViewJavascriptFetchQueyCommand]]; - [_base _flushMessageQueue:messageQueueString]; + [_base flushMessageQueue:messageQueueString]; } else { [_base logUnkownMessage:url]; } diff --git a/WebViewJavascriptBridge/WebViewJavascriptBridgeBase.h b/WebViewJavascriptBridge/WebViewJavascriptBridgeBase.h index 310a0b8f..d2539d83 100644 --- a/WebViewJavascriptBridge/WebViewJavascriptBridgeBase.h +++ b/WebViewJavascriptBridge/WebViewJavascriptBridgeBase.h @@ -1,10 +1,10 @@ // // WebViewJavascriptBridgeBase.h -// ExampleApp-iOS // -// Created by Loki Meyburg on 2014-10-29. -// Copyright (c) 2014 Marcus Westin. All rights reserved. +// Created by @LokiMeyburg on 10/15/14. +// Copyright (c) 2014 @LokiMeyburg. All rights reserved. // + #import #define kCustomProtocolScheme @"wvjbscheme" @@ -14,17 +14,12 @@ typedef void (^WVJBResponseCallback)(id responseData); typedef void (^WVJBHandler)(id data, WVJBResponseCallback responseCallback); typedef NSDictionary WVJBMessage; - -// setup delegate @protocol WebViewJavascriptBridgeBaseDelegate - (NSString*) _evaluateJavascript:(NSString*)javascriptCommand; @end - - @interface WebViewJavascriptBridgeBase : NSObject -// Delegate property @property (assign) id delegate; @property (strong, nonatomic) NSMutableArray* startupMessageQueue; @property (strong, nonatomic) NSMutableDictionary* responseCallbacks; @@ -32,29 +27,17 @@ typedef NSDictionary WVJBMessage; @property (strong, nonatomic) WVJBHandler messageHandler; @property NSUInteger numRequestsLoading; - + (void)enableLogging; --(id)initWithWebViewType:(NSString*)webViewType handler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle; --(void)reset; -- (void)_sendData:(id)data responseCallback:(WVJBResponseCallback)responseCallback handlerName:(NSString*)handlerName; -- (void)_queueMessage:(WVJBMessage*)message; -- (void)_dispatchMessage:(WVJBMessage*)message; -- (void)_flushMessageQueue:(NSString *)messageQueueString; - -// specific extractions -- (void) injectJavascriptFile:(BOOL)shouldInject; --(BOOL) correctProcotocolScheme:(NSURL*)url; --(BOOL) correctHost:(NSURL*)urll; --(void) logUnkownMessage:(NSURL*)url; --(NSString *) webViewJavascriptCheckCommand; --(NSString *) webViewJavascriptFetchQueyCommand; -- (void) dispatchStartUpMessageQueue; - - -// probably dont need to be public -- (NSString *)_serializeMessage:(id)message; -- (NSArray*)_deserializeMessageJSON:(NSString *)messageJSON; -- (void)_log:(NSString *)action json:(id)json; - +- (id)initWithHandler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle; +- (void)reset; +- (void)sendData:(id)data responseCallback:(WVJBResponseCallback)responseCallback handlerName:(NSString*)handlerName; +- (void)flushMessageQueue:(NSString *)messageQueueString; +- (void)injectJavascriptFile:(BOOL)shouldInject; +- (BOOL)isCorrectProcotocolScheme:(NSURL*)url; +- (BOOL)isCorrectHost:(NSURL*)urll; +- (void)logUnkownMessage:(NSURL*)url; +- (void)dispatchStartUpMessageQueue; +- (NSString *)webViewJavascriptCheckCommand; +- (NSString *)webViewJavascriptFetchQueyCommand; @end \ No newline at end of file diff --git a/WebViewJavascriptBridge/WebViewJavascriptBridgeBase.m b/WebViewJavascriptBridge/WebViewJavascriptBridgeBase.m index 40fce247..3717eb7d 100644 --- a/WebViewJavascriptBridge/WebViewJavascriptBridgeBase.m +++ b/WebViewJavascriptBridge/WebViewJavascriptBridgeBase.m @@ -1,9 +1,8 @@ // // WebViewJavascriptBridgeBase.m -// ExampleApp-iOS // -// Created by Loki Meyburg on 2014-10-29. -// Copyright (c) 2014 Marcus Westin. All rights reserved. +// Created by @LokiMeyburg on 10/15/14. +// Copyright (c) 2014 @LokiMeyburg. All rights reserved. // #import @@ -13,19 +12,15 @@ @implementation WebViewJavascriptBridgeBase { id _webViewDelegate; long _uniqueId; NSBundle *_resourceBundle; - // set the webview type in a string - NSString *_webViewType; } - static bool logging = false; -+ (void)enableLogging { logging = true; } ++ (void)enableLogging { logging = true; } --(id)initWithWebViewType:(NSString*)webViewType handler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle +-(id)initWithHandler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle { self = [super init]; - _webViewType = webViewType; _resourceBundle = bundle; self.messageHandler = messageHandler; self.messageHandlers = [NSMutableDictionary dictionary]; @@ -48,8 +43,7 @@ - (void)reset { _uniqueId = 0; } - -- (void)_sendData:(id)data responseCallback:(WVJBResponseCallback)responseCallback handlerName:(NSString*)handlerName { +- (void)sendData:(id)data responseCallback:(WVJBResponseCallback)responseCallback handlerName:(NSString*)handlerName { NSMutableDictionary* message = [NSMutableDictionary dictionary]; if (data) { @@ -68,40 +62,7 @@ - (void)_sendData:(id)data responseCallback:(WVJBResponseCallback)responseCallba [self _queueMessage:message]; } - -- (void)_queueMessage:(WVJBMessage*)message { - if (self.startupMessageQueue) { - [self.startupMessageQueue addObject:message]; - } else { - [self _dispatchMessage:message]; - } -} - -- (void)_dispatchMessage:(WVJBMessage*)message { - NSString *messageJSON = [self _serializeMessage:message]; - [self _log:@"SEND" json:messageJSON]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\r" withString:@"\\r"]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\f" withString:@"\\f"]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\u2028" withString:@"\\u2028"]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\u2029" withString:@"\\u2029"]; - - NSString* javascriptCommand = [NSString stringWithFormat:@"WebViewJavascriptBridge._handleMessageFromObjC('%@');", messageJSON]; - if ([[NSThread currentThread] isMainThread]) { - [self _evaluateJavascript:javascriptCommand]; - - } else { - dispatch_sync(dispatch_get_main_queue(), ^{ - [self _evaluateJavascript:javascriptCommand]; - }); - } -} - - -- (void)_flushMessageQueue:(NSString *)messageQueueString{ +- (void)flushMessageQueue:(NSString *)messageQueueString{ id messages = [self _deserializeMessageJSON:messageQueueString]; if (![messages isKindOfClass:[NSArray class]]) { NSLog(@"WebViewJavascriptBridge: WARNING: Invalid %@ received: %@", [messages class], messages); @@ -153,43 +114,19 @@ - (void)_flushMessageQueue:(NSString *)messageQueueString{ } } -- (NSString *)_serializeMessage:(id)message { - return [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:message options:0 error:nil] encoding:NSUTF8StringEncoding]; -} - -- (NSArray*)_deserializeMessageJSON:(NSString *)messageJSON { - return [NSJSONSerialization JSONObjectWithData:[messageJSON dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil]; -} - -- (void)_log:(NSString *)action json:(id)json { - if (!logging) { return; } - if (![json isKindOfClass:[NSString class]]) { - json = [self _serializeMessage:json]; - } - if ([json length] > 500) { - NSLog(@"WVJB %@: %@ [...]", action, [json substringToIndex:500]); - } else { - NSLog(@"WVJB %@: %@", action, json); - } -} - - - (void)injectJavascriptFile:(BOOL)shouldInject { - if(!shouldInject){ + if(shouldInject){ NSBundle *bundle = _resourceBundle ? _resourceBundle : [NSBundle mainBundle]; NSString *filePath = [bundle pathForResource:@"WebViewJavascriptBridge.js" ofType:@"txt"]; NSString *js = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; [self _evaluateJavascript:js]; [self dispatchStartUpMessageQueue]; } - + } -- (void) dispatchStartUpMessageQueue { - NSLog(@"0. Dispathing queue"); +- (void)dispatchStartUpMessageQueue { if (self.startupMessageQueue) { - NSLog(@"1. Startup queue found"); - for (id queuedMessage in self.startupMessageQueue) { [self _dispatchMessage:queuedMessage]; } @@ -197,7 +134,7 @@ - (void) dispatchStartUpMessageQueue { } } --(BOOL)correctProcotocolScheme:(NSURL*)url { +-(BOOL)isCorrectProcotocolScheme:(NSURL*)url { if([[url scheme] isEqualToString:kCustomProtocolScheme]){ return YES; } else { @@ -205,8 +142,7 @@ -(BOOL)correctProcotocolScheme:(NSURL*)url { } } - --(BOOL)correctHost:(NSURL*)url { +-(BOOL)isCorrectHost:(NSURL*)url { if([[url host] isEqualToString:kQueueHasMessage]){ return YES; } else { @@ -214,25 +150,74 @@ -(BOOL)correctHost:(NSURL*)url { } } - --(void) logUnkownMessageFor:(NSURL*)url { +-(void)logUnkownMessage:(NSURL*)url { NSLog(@"WebViewJavascriptBridge: WARNING: Received unknown WebViewJavascriptBridge command %@://%@", kCustomProtocolScheme, [url path]); } - --(NSString *) webViewJavascriptCheckCommand { +-(NSString *)webViewJavascriptCheckCommand { return @"typeof WebViewJavascriptBridge == \'object\';"; } --(NSString *) webViewJavascriptFetchQueyCommand { +-(NSString *)webViewJavascriptFetchQueyCommand { return @"WebViewJavascriptBridge._fetchQueue();"; } +// Private +// ------------------------------------------- + - (void) _evaluateJavascript:(NSString *)javascriptCommand { [self.delegate _evaluateJavascript:javascriptCommand]; } +- (void)_queueMessage:(WVJBMessage*)message { + if (self.startupMessageQueue) { + [self.startupMessageQueue addObject:message]; + } else { + [self _dispatchMessage:message]; + } +} + +- (void)_dispatchMessage:(WVJBMessage*)message { + NSString *messageJSON = [self _serializeMessage:message]; + [self _log:@"SEND" json:messageJSON]; + messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"]; + messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]; + messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"]; + messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"]; + messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\r" withString:@"\\r"]; + messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\f" withString:@"\\f"]; + messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\u2028" withString:@"\\u2028"]; + messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\u2029" withString:@"\\u2029"]; + + NSString* javascriptCommand = [NSString stringWithFormat:@"WebViewJavascriptBridge._handleMessageFromObjC('%@');", messageJSON]; + if ([[NSThread currentThread] isMainThread]) { + [self _evaluateJavascript:javascriptCommand]; + + } else { + dispatch_sync(dispatch_get_main_queue(), ^{ + [self _evaluateJavascript:javascriptCommand]; + }); + } +} + +- (NSString *)_serializeMessage:(id)message { + return [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:message options:0 error:nil] encoding:NSUTF8StringEncoding]; +} +- (NSArray*)_deserializeMessageJSON:(NSString *)messageJSON { + return [NSJSONSerialization JSONObjectWithData:[messageJSON dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil]; +} +- (void)_log:(NSString *)action json:(id)json { + if (!logging) { return; } + if (![json isKindOfClass:[NSString class]]) { + json = [self _serializeMessage:json]; + } + if ([json length] > 500) { + NSLog(@"WVJB %@: %@ [...]", action, [json substringToIndex:500]); + } else { + NSLog(@"WVJB %@: %@", action, json); + } +} @end \ No newline at end of file