Skip to content

Commit b6580b7

Browse files
author
Ali Najafizadeh
committed
Merge pull request #75 from Bhullnatik/master
Add support for Android 1(6)+
2 parents cba2f20 + c439445 commit b6580b7

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

android/src/main/java/com/github/alinz/reactnativewebviewbridge/WebViewBridgeManager.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package com.github.alinz.reactnativewebviewbridge;
22

3-
import javax.annotation.Nullable;
4-
import java.util.Map;
5-
63
import android.webkit.WebView;
74

5+
import com.facebook.react.bridge.ReactContext;
6+
import com.facebook.react.bridge.ReadableArray;
87
import com.facebook.react.views.webview.ReactWebViewManager;
98
import com.facebook.react.views.webview.WebViewConfig;
10-
import com.facebook.react.bridge.ReadableArray;
11-
import com.facebook.react.bridge.ReactContext;
12-
import com.facebook.react.bridge.WritableMap;
13-
import com.facebook.react.common.MapBuilder;
9+
10+
import java.util.Map;
11+
12+
import javax.annotation.Nullable;
1413

1514
public class WebViewBridgeManager extends ReactWebViewManager {
1615
private static final String REACT_CLASS = "RCTWebViewBridge";
@@ -64,29 +63,37 @@ public void receiveCommand(WebView root, int commandId, @Nullable ReadableArray
6463
private void sendToBridge(WebView root, String message) {
6564
//root.loadUrl("javascript:(function() {\n" + script + ";\n})();");
6665
String script = "WebViewBridge.onMessage('" + message + "');";
67-
root.evaluateJavascript(script, null);
66+
WebViewBridgeManager.evaluateJavascript(root, script);
6867
}
6968

7069
private void injectBridgeScript(WebView root) {
7170
//this code needs to be called once per context
7271
if (!initializedBridge) {
73-
root.addJavascriptInterface(new JavascriptBridge((ReactContext)root.getContext()), "WebViewBridgeAndroid");
72+
root.addJavascriptInterface(new JavascriptBridge((ReactContext) root.getContext()), "WebViewBridgeAndroid");
7473
initializedBridge = true;
7574
root.reload();
7675
}
7776

78-
//this code needs to be executed everytime a url changes.
79-
root.evaluateJavascript(""
80-
+ "(function() {"
81-
+ "if (window.WebViewBridge) return;"
82-
+ "var customEvent = document.createEvent('Event');"
83-
+ "var WebViewBridge = {"
84-
+ "send: function(message) { WebViewBridgeAndroid.send(message); },"
85-
+ "onMessage: function() {}"
86-
+ "};"
87-
+ "window.WebViewBridge = WebViewBridge;"
88-
+ "customEvent.initEvent('WebViewBridge', true, true);"
89-
+ "document.dispatchEvent(customEvent);"
90-
+"}());", null);
77+
// this code needs to be executed everytime a url changes.
78+
WebViewBridgeManager.evaluateJavascript(root, ""
79+
+ "(function() {"
80+
+ "if (window.WebViewBridge) return;"
81+
+ "var customEvent = document.createEvent('Event');"
82+
+ "var WebViewBridge = {"
83+
+ "send: function(message) { WebViewBridgeAndroid.send(message); },"
84+
+ "onMessage: function() {}"
85+
+ "};"
86+
+ "window.WebViewBridge = WebViewBridge;"
87+
+ "customEvent.initEvent('WebViewBridge', true, true);"
88+
+ "document.dispatchEvent(customEvent);"
89+
+ "}());");
90+
}
91+
92+
static private void evaluateJavascript(WebView root, String javascript) {
93+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
94+
root.evaluateJavascript(javascript, null);
95+
} else {
96+
root.loadUrl("javascript:" + javascript);
97+
}
9198
}
9299
}

0 commit comments

Comments
 (0)