From 132a4a1846846d94033e0dbc9595c1d42e95d7bb Mon Sep 17 00:00:00 2001 From: Ali Najafizadeh Date: Tue, 8 Dec 2015 17:16:58 -0500 Subject: [PATCH] adding auto injection --- example/Sample2/lib/ios/RCTWebViewBridge.h | 2 +- example/Sample2/lib/ios/RCTWebViewBridge.m | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/example/Sample2/lib/ios/RCTWebViewBridge.h b/example/Sample2/lib/ios/RCTWebViewBridge.h index e78dbb1..743640a 100644 --- a/example/Sample2/lib/ios/RCTWebViewBridge.h +++ b/example/Sample2/lib/ios/RCTWebViewBridge.h @@ -42,6 +42,6 @@ shouldStartLoadForRequest:(NSMutableDictionary *)request - (void)goForward; - (void)goBack; - (void)reload; -- (void)sendToBridge: NSString *message; +- (void)sendToBridge:(NSString *)message; @end diff --git a/example/Sample2/lib/ios/RCTWebViewBridge.m b/example/Sample2/lib/ios/RCTWebViewBridge.m index b7a06b4..f4151a2 100644 --- a/example/Sample2/lib/ios/RCTWebViewBridge.m +++ b/example/Sample2/lib/ios/RCTWebViewBridge.m @@ -21,7 +21,7 @@ #import "RCTView.h" #import "UIView+React.h" -//This is very elegent way of defining multiline string on objective-c. +//This is a very elegent way of defining multiline string in objective-c. //source: http://stackoverflow.com/a/23387659/828487 #define NSStringMultiline(...) [[NSString alloc] initWithCString:#__VA_ARGS__ encoding:NSUTF8StringEncoding] @@ -76,7 +76,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder) [_webView reload]; } -- (void)sendToBridge: NSString *message +- (void)sendToBridge:(NSString *)message { //we are warpping the send message in a function to make sure that if //WebView is not injected, we don't crash the app. @@ -238,6 +238,15 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder) - (void)webViewDidFinishLoad:(UIWebView *)webView { + //injecting WebViewBridge Script + NSString *webViewBridgeScriptFile = [bundle pathForResource:@"webviewbridge" + ofType:@"js"]; + NSString *webViewBridgeScriptContent = [NSString stringWithContentsOfFile:webViewBridgeScriptFile + encoding:NSUTF8StringEncoding + error:nil]; + [webView stringByEvaluatingJavaScriptFromString:webViewBridgeScriptContent]; + ////////////////////////////////////////////////////////////////////////////// + if (_injectedJavaScript != nil) { NSString *jsEvaluationValue = [webView stringByEvaluatingJavaScriptFromString:_injectedJavaScript];