Skip to content

Commit

Permalink
Removed compiler warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
lokimeyburg committed Nov 9, 2014
1 parent 1bce38a commit 4e990fb
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 147 deletions.
10 changes: 4 additions & 6 deletions WebViewJavascriptBridge/WKWebViewJavascriptBridge.h
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -12,12 +12,10 @@
#if defined(supportsWKWebKit )

#import <Foundation/Foundation.h>
#import "WebViewJavascriptBridgeBase.h"
#import <WebKit/WebKit.h>

typedef void (^WVJBResponseCallback)(id responseData);
typedef void (^WVJBHandler)(id data, WVJBResponseCallback responseCallback);

@interface WKWebViewJavascriptBridge : NSObject<WKNavigationDelegate>
@interface WKWebViewJavascriptBridge : NSObject<WKNavigationDelegate, WebViewJavascriptBridgeBaseDelegate>

+ (instancetype)bridgeForWebView:(WKWebView*)webView handler:(WVJBHandler)handler;
+ (instancetype)bridgeForWebView:(WKWebView*)webView webViewDelegate:(NSObject<WKNavigationDelegate>*)webViewDelegate handler:(WVJBHandler)handler;
Expand Down
19 changes: 9 additions & 10 deletions WebViewJavascriptBridge/WKWebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -85,14 +84,14 @@ - (void) _setupInstance:(WKWebView*)webView webViewDelegate:(id<WKNavigationDele
_webView = webView;
_webViewDelegate = webViewDelegate;
_webView.navigationDelegate = self;
_base = [[WebViewJavascriptBridgeBase alloc] initWithWebViewType:@"WKWebView" handler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle];
_base = [[WebViewJavascriptBridgeBase alloc] initWithHandler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle];
_base.delegate = self;
}


- (void)WKFlushMessageQueue {
[_webView evaluateJavaScript:[_base webViewJavascriptFetchQueyCommand] completionHandler:^(NSString* result, NSError* error) {
[_base _flushMessageQueue:result];
[_base flushMessageQueue:result];
}];
}

Expand All @@ -104,7 +103,7 @@ - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigat

if (_base.numRequestsLoading == 0) {
[webView evaluateJavaScript:[_base webViewJavascriptCheckCommand] completionHandler:^(NSString *result, NSError *error) {
[_base injectJavascriptFile:[result boolValue]];
[_base injectJavascriptFile:![result boolValue]];
}];
}

Expand All @@ -122,8 +121,8 @@ - (void)webView:(WKWebView *)webView
NSURL *url = navigationAction.request.URL;
__strong typeof(_webViewDelegate) strongDelegate = _webViewDelegate;

if ([_base correctProcotocolScheme:url]) {
if ([_base correctHost:url]) {
if ([_base isCorrectProcotocolScheme:url]) {
if ([_base isCorrectHost:url]) {
[self WKFlushMessageQueue];
} else {
[_base logUnkownMessage:url];
Expand Down
8 changes: 3 additions & 5 deletions WebViewJavascriptBridge/WebViewJavascriptBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@
//

#import <Foundation/Foundation.h>
#import "WebViewJavascriptBridgeBase.h"

#if defined __MAC_OS_X_VERSION_MAX_ALLOWED
#import <WebKit/WebKit.h>
#define WVJB_PLATFORM_OSX
#define WVJB_WEBVIEW_TYPE WebView
#define WVJB_WEBVIEW_DELEGATE_TYPE NSObject
#define WVJB_WEBVIEW_DELEGATE_TYPE NSObject<WebViewJavascriptBridgeBaseDelegate>
#elif defined __IPHONE_OS_VERSION_MAX_ALLOWED
#import <UIKit/UIWebView.h>
#define WVJB_PLATFORM_IOS
#define WVJB_WEBVIEW_TYPE UIWebView
#define WVJB_WEBVIEW_DELEGATE_TYPE NSObject<UIWebViewDelegate>
#define WVJB_WEBVIEW_DELEGATE_TYPE NSObject<UIWebViewDelegate, WebViewJavascriptBridgeBaseDelegate>
#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;
Expand Down
27 changes: 12 additions & 15 deletions WebViewJavascriptBridge/WebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
//

#import "WebViewJavascriptBridge.h"
#import "WebViewJavascriptBridgeBase.h"

#if __has_feature(objc_arc_weak)
#define WVJB_WEAK __weak
#else
#define WVJB_WEAK __unsafe_unretained
#endif

typedef NSDictionary WVJBMessage;

@implementation WebViewJavascriptBridge {
WVJB_WEAK WVJB_WEBVIEW_TYPE* _webView;
WVJB_WEAK id _webViewDelegate;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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];
Expand All @@ -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];
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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];

Expand All @@ -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];
}
Expand Down
45 changes: 14 additions & 31 deletions WebViewJavascriptBridge/WebViewJavascriptBridgeBase.h
Original file line number Diff line number Diff line change
@@ -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 <Foundation/Foundation.h>

#define kCustomProtocolScheme @"wvjbscheme"
Expand All @@ -14,47 +14,30 @@ typedef void (^WVJBResponseCallback)(id responseData);
typedef void (^WVJBHandler)(id data, WVJBResponseCallback responseCallback);
typedef NSDictionary WVJBMessage;


// setup delegate
@protocol WebViewJavascriptBridgeBaseDelegate <NSObject>
- (NSString*) _evaluateJavascript:(NSString*)javascriptCommand;
@end



@interface WebViewJavascriptBridgeBase : NSObject

// Delegate property
@property (assign) id <WebViewJavascriptBridgeBaseDelegate> delegate;
@property (strong, nonatomic) NSMutableArray* startupMessageQueue;
@property (strong, nonatomic) NSMutableDictionary* responseCallbacks;
@property (strong, nonatomic) NSMutableDictionary* messageHandlers;
@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
Loading

0 comments on commit 4e990fb

Please sign in to comment.