2015-06-08 14:08:54 -04:00
|
|
|
/**
|
2015-06-19 15:48:50 -04:00
|
|
|
* Copyright (c) 2015-present, Ali Najafizadeh.
|
|
|
|
|
* Ali Najafizadeh
|
|
|
|
|
* MIT
|
2015-06-08 14:08:54 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#import "WebViewEx.h"
|
|
|
|
|
|
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
|
|
|
|
|
#import "RCTAutoInsetsProtocol.h"
|
|
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
|
#import "RCTLog.h"
|
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
#import "RCTView.h"
|
|
|
|
|
#import "UIView+React.h"
|
|
|
|
|
|
2015-06-08 16:52:07 -04:00
|
|
|
#import "WebViewJavascriptBridge.h"
|
|
|
|
|
|
2015-06-08 14:08:54 -04:00
|
|
|
|
|
|
|
|
@implementation WebViewEx
|
|
|
|
|
{
|
2015-06-08 16:52:07 -04:00
|
|
|
WebViewJavascriptBridge* _bridge;
|
|
|
|
|
RCTResponseSenderBlock _callback;
|
2015-06-24 14:21:45 -04:00
|
|
|
UIWebView* _webview;
|
2015-06-08 14:08:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
|
|
|
|
|
{
|
2015-06-19 15:48:50 -04:00
|
|
|
self = [super initWithEventDispatcher:eventDispatcher];
|
|
|
|
|
|
|
|
|
|
if (self != nil)
|
|
|
|
|
{
|
|
|
|
|
for (UIView *subview in [self subviews])
|
|
|
|
|
{
|
|
|
|
|
if ([subview isKindOfClass: [UIWebView class]])
|
|
|
|
|
{
|
2015-06-24 14:21:45 -04:00
|
|
|
_webview = (UIWebView *)subview;
|
|
|
|
|
_bridge = [WebViewJavascriptBridge bridgeForWebView:_webview handler:^(id data, WVJBResponseCallback responseCallback) {
|
2015-06-19 15:48:50 -04:00
|
|
|
//NSLog(@"Received message from javascript: %@", data);
|
|
|
|
|
_callback(@[data]);
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-08 14:08:54 -04:00
|
|
|
}
|
2015-06-19 15:48:50 -04:00
|
|
|
|
2015-06-08 14:08:54 -04:00
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-24 14:21:45 -04:00
|
|
|
- (void)eval:(NSString *) value {
|
|
|
|
|
[_webview stringByEvaluatingJavaScriptFromString:value];
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-08 16:31:20 -04:00
|
|
|
- (void)send:(id)message
|
|
|
|
|
{
|
2015-06-08 16:52:07 -04:00
|
|
|
[_bridge send:message];
|
2015-06-09 09:24:19 -04:00
|
|
|
//_callback(@[message]);
|
2015-06-08 16:52:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)onMessage:(RCTResponseSenderBlock)callback
|
|
|
|
|
{
|
|
|
|
|
_callback = callback;
|
2015-06-08 16:31:20 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-19 15:48:50 -04:00
|
|
|
@end
|