alinz / react-native-webview-bridge

React Native Webview with Javascript Bridge
MIT License
1.37k stars 493 forks source link

Communication between web view and react native code not working in iOS but working on android #261

Open singhlovey opened 6 years ago

singhlovey commented 6 years ago

Hi

I am opening dropbox loin screen in web view bridge. Now when I successfully login to dropbox and got access token I want to send success message back to react native code. I have successfully done this step for android app.

REACT NATIVE CODE :-

onBridgeMessage(message) { const { webviewbridge } = this.refs; console.log("message " + JSON.stringify(message)); switch (message) { case "hello from webview": webviewbridge.sendToBridge("hello from react-native"); break; case "success": AsyncStorage.setItem("socialicon", "dropbox"); this.props.navigator.push({name: 'GoogleSigninExample', index: 0 }); break;
//console.log("we have got a message from webview! yeah"); case "error": AsyncStorage.setItem("socialicon", "dropboxerror"); this.props.navigator.push({name: 'GoogleSigninExample', index: 0 });
break; } }

WEBVIEW CODE :-

             echo'<script language="javascript">
                      (function () {
                        if (WebViewBridge) {

                          WebViewBridge.onMessage = function (message) {
                            if (message === "hello from react-native") {
                              WebViewBridge.send("success");
                            }
                          };
                          WebViewBridge.send("hello from webview");
                        }
                      }());
            </script>
            ';

This works perfect and I am able to get success message from web view into react native in android app.

But the same does not works for iOS app. I have checked in simulator as well as in real device but script not triggered.

NOTE:- When I inject script from react native using injectedScript props, I am able to get message on server end but I want like the above mentioned.

Please help if anyone have any solution for the same. Please reply ASAP!

Thanks

simonso-nzme commented 5 years ago

I am getting similar issue. Works on Android but not iOS, injectJS works but not server side. When I log the try and catch error, I got: ReferenceError: Can't find variable WebViewBridge

react-native 0.57.1 react-native-webview-bridge 0.40.1

simonso-nzme commented 5 years ago

Ok, we got it going at the end. I believe it is a timing/initiation issue. What we end up doing is:

React Native: <WebViewBridge injectedJavaScript="window.configureWebView(WebViewBridge)" />

Web: window.configureWebView = (vBridge) => { vBridge.onMessage = (data) => { ... } }

hkanizawa commented 5 years ago

@singhlovey I'm with the same problem here. Did you solve this?

djsupersonicdesign commented 5 years ago

Hi anyone solve this?

fungilation commented 5 years ago

No, this repo is dead to me. I've migrated completely to https://github.com/react-native-community/react-native-webview which is the new official webview of RN, it's actively developed and it does everything that react-native-webview-bridge did. I suggest you to migrate as well.