diff --git a/.gitignore b/.gitignore index 3c3629e..fd4f2b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.DS_Store diff --git a/WebViewJavascriptBridge/WebViewEx.h b/WebViewJavascriptBridge/WebViewEx.h index 6122069..0c15dc1 100644 --- a/WebViewJavascriptBridge/WebViewEx.h +++ b/WebViewJavascriptBridge/WebViewEx.h @@ -23,5 +23,6 @@ - (void)goForward; - (void)goBack; - (void)reload; +- (void)send:(id)message; @end \ No newline at end of file diff --git a/WebViewJavascriptBridge/WebViewEx.m b/WebViewJavascriptBridge/WebViewEx.m index ede495a..7bab17e 100644 --- a/WebViewJavascriptBridge/WebViewEx.m +++ b/WebViewJavascriptBridge/WebViewEx.m @@ -57,6 +57,11 @@ [_webView reload]; } +- (void)send:(id)message +{ + NSLog(@"Message got %@", message); +} + - (void)setURL:(NSURL *)URL { // Because of the way React works, as pages redirect, we actually end up diff --git a/WebViewJavascriptBridge/WebViewExManager.m b/WebViewJavascriptBridge/WebViewExManager.m index 40db627..7ac286a 100644 --- a/WebViewJavascriptBridge/WebViewExManager.m +++ b/WebViewJavascriptBridge/WebViewExManager.m @@ -12,7 +12,7 @@ #import "RCTBridge.h" #import "RCTSparseArray.h" #import "RCTUIManager.h" -#import "RCTWebView.h" +#import "WebViewEx.h" @implementation WebViewExManager @@ -20,7 +20,7 @@ RCT_EXPORT_MODULE() - (UIView *)view { - return [[RCTWebView alloc] initWithEventDispatcher:self.bridge.eventDispatcher]; + return [[WebViewEx alloc] initWithEventDispatcher:self.bridge.eventDispatcher]; } RCT_REMAP_VIEW_PROPERTY(url, URL, NSURL); @@ -48,8 +48,8 @@ RCT_EXPORT_VIEW_PROPERTY(shouldInjectAJAXHandler, BOOL); RCT_EXPORT_METHOD(goBack:(NSNumber *)reactTag) { [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry) { - RCTWebView *view = viewRegistry[reactTag]; - if (![view isKindOfClass:[RCTWebView class]]) { + WebViewEx *view = viewRegistry[reactTag]; + if (![view isKindOfClass:[WebViewEx class]]) { RCTLogError(@"Invalid view returned from registry, expecting RKWebView, got: %@", view); } [view goBack]; @@ -60,7 +60,7 @@ RCT_EXPORT_METHOD(goForward:(NSNumber *)reactTag) { [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry) { id view = viewRegistry[reactTag]; - if (![view isKindOfClass:[RCTWebView class]]) { + if (![view isKindOfClass:[WebViewEx class]]) { RCTLogError(@"Invalid view returned from registry, expecting RKWebView, got: %@", view); } [view goForward]; @@ -71,12 +71,29 @@ RCT_EXPORT_METHOD(goForward:(NSNumber *)reactTag) RCT_EXPORT_METHOD(reload:(NSNumber *)reactTag) { [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry) { - RCTWebView *view = viewRegistry[reactTag]; - if (![view isKindOfClass:[RCTWebView class]]) { + WebViewEx *view = viewRegistry[reactTag]; + if (![view isKindOfClass:[WebViewEx class]]) { RCTLogMustFix(@"Invalid view returned from registry, expecting RKWebView, got: %@", view); } [view reload]; }]; } +RCT_EXPORT_METHOD(onMessage:(RCTResponseSenderBlock) callback) +{ + NSLog(@"Called"); +} + +RCT_EXPORT_METHOD(send:(NSNumber *)reactTag :(id)message) +{ + [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry) { + WebViewEx *view = viewRegistry[reactTag]; + if (![view isKindOfClass:[WebViewEx class]]) { + RCTLogMustFix(@"Invalid view returned from registry, expecting RKWebView, got: %@", view); + } + [view send:message]; + }]; +} + + @end \ No newline at end of file diff --git a/jsx/index.ios.js b/jsx/index.ios.js index fda42f2..0dd8f3d 100644 --- a/jsx/index.ios.js +++ b/jsx/index.ios.js @@ -15,9 +15,16 @@ var { } = React; var webview = React.createClass({ + componentDidMount: function () { + this.refs.myWebView.onMessage(function () { + console.log('called from objective c'); + }); + + this.refs.myWebView.send("HELLO"); + }, render: function() { return ( - + ); } }); diff --git a/jsx/webviewex.js b/jsx/webviewex.js index 60c7d17..5249e4a 100644 --- a/jsx/webviewex.js +++ b/jsx/webviewex.js @@ -73,7 +73,7 @@ var defaultRenderError = (errorDomain, errorCode, errorDesc) => ( ); -var WebViewEx = React.createClass({ +var WebView = React.createClass({ statics: { NavigationType: NavigationType, }, @@ -177,6 +177,14 @@ var WebViewEx = React.createClass({ WebViewExManager.reload(this.getWebWiewHandle()); }, + onMessage: function (cb) { + WebViewExManager.onMessage(cb); + }, + + send: function (message) { + WebViewExManager.send(this.getWebWiewHandle(), message); + }, + /** * We return an event with a bunch of fields including: * url, title, loading, canGoBack, canGoForward @@ -213,7 +221,7 @@ var WebViewEx = React.createClass({ }, }); -var WebViewEx = requireNativeComponent('WebViewEx', WebViewEx); +var WebViewEx = requireNativeComponent('WebViewEx', WebView); var styles = StyleSheet.create({ container: { @@ -250,4 +258,4 @@ var styles = StyleSheet.create({ } }); -module.exports = WebViewEx; +module.exports = WebView; diff --git a/webview.xcodeproj/project.xcworkspace/xcuserdata/ali.xcuserdatad/UserInterfaceState.xcuserstate b/webview.xcodeproj/project.xcworkspace/xcuserdata/ali.xcuserdatad/UserInterfaceState.xcuserstate index db0d40b..da5e542 100644 Binary files a/webview.xcodeproj/project.xcworkspace/xcuserdata/ali.xcuserdatad/UserInterfaceState.xcuserstate and b/webview.xcodeproj/project.xcworkspace/xcuserdata/ali.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/webview.xcodeproj/xcuserdata/ali.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/webview.xcodeproj/xcuserdata/ali.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index fe2b454..7d11131 100644 --- a/webview.xcodeproj/xcuserdata/ali.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/webview.xcodeproj/xcuserdata/ali.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -2,4 +2,22 @@ + + + + + +