Skip to content

Commit

Permalink
WKWebViewJavascriptBridge successfully using Base class with delegate…
Browse files Browse the repository at this point in the history
… JS execution
  • Loading branch information
lokimeyburg committed Nov 8, 2014
1 parent 859c823 commit b318263
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
3 changes: 3 additions & 0 deletions WebViewJavascriptBridge/WKWebViewJavascriptBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ typedef void (^WVJBHandler)(id data, WVJBResponseCallback responseCallback);
- (void)callHandler:(NSString*)handlerName data:(id)data responseCallback:(WVJBResponseCallback)responseCallback;
- (void)reset;

// delegate method
- (void) _evaluateJavascript:(NSString*)javascriptCommand;

@end

#endif
8 changes: 8 additions & 0 deletions WebViewJavascriptBridge/WKWebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ @implementation WKWebViewJavascriptBridge {
WebViewJavascriptBridgeBase *_base;
}

//@synthesize delegate;

/* API
*****/

Expand Down Expand Up @@ -86,6 +88,7 @@ - (void) _platformSpecificSetup:(WKWebView*)webView webViewDelegate:(id<WKNaviga
_webViewDelegate = webViewDelegate;
_webView.navigationDelegate = self;
_base = [[WebViewJavascriptBridgeBase alloc] initWithWebViewType:@"WKWebView" handler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle];
_base.delegate = self;
}


Expand Down Expand Up @@ -162,6 +165,11 @@ - (void)webView:(WKWebView *)webView
}
}

- (void) _evaluateJavascript:(NSString*)javascriptCommand
{
[_webView evaluateJavaScript:javascriptCommand completionHandler:nil];
}



@end
Expand Down
10 changes: 10 additions & 0 deletions WebViewJavascriptBridge/WebViewJavascriptBridgeBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ typedef void (^WVJBResponseCallback)(id responseData);
typedef void (^WVJBHandler)(id data, WVJBResponseCallback responseCallback);
typedef NSDictionary WVJBMessage;


// setup delegate
@protocol WebViewJavascriptBridgeBaseDelegate <NSObject>
- (void) _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;
Expand Down
8 changes: 4 additions & 4 deletions WebViewJavascriptBridge/WebViewJavascriptBridgeBase.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ - (void)_dispatchMessage:(WVJBMessage*)message {
}


- (void) _evaluateJavascript:(NSString *)javascriptCommand {
NSLog(@"----> TODO: Do this method! DEBUG: Evaluating javascript!");
}

- (void)_flushMessageQueue:(NSString *)messageQueueString{
id messages = [self _deserializeMessageJSON:messageQueueString];
if (![messages isKindOfClass:[NSArray class]]) {
Expand Down Expand Up @@ -223,6 +219,10 @@ -(NSString *) webViewJavascriptFetchQueyCommand {
return @"WebViewJavascriptBridge._fetchQueue();";
}

- (void) _evaluateJavascript:(NSString *)javascriptCommand {
[self.delegate _evaluateJavascript:javascriptCommand];
}




Expand Down

0 comments on commit b318263

Please sign in to comment.