How to communcation between web inside webview back to react native apps












0















I'm new in React Native.



I need to create communication between RN - Web - RN (RN to Web and then Web back to RN).



Assume i have list of item inside my web that called inside a webview. Each item inside list can be tapped/clicked. When i click one of item inside item list inside my web, i will redirect it to React Native apps page and show detail of realted item.



What i have done until now :
I can inject Javascript from React Native to Webview like this :



<WebView
source={{uri: "myweb.com/webview.php"}}
injectedJavaScript={jsCode}
javaScriptEnabledAndroid={true}
style={{height: 100}} />


That code can open communication between React Native to Webpage inside webview, but i still fail to communication from Web inside webview to React Native. Can anyone explain, how to do it (and is it possible?)?










share|improve this question





























    0















    I'm new in React Native.



    I need to create communication between RN - Web - RN (RN to Web and then Web back to RN).



    Assume i have list of item inside my web that called inside a webview. Each item inside list can be tapped/clicked. When i click one of item inside item list inside my web, i will redirect it to React Native apps page and show detail of realted item.



    What i have done until now :
    I can inject Javascript from React Native to Webview like this :



    <WebView
    source={{uri: "myweb.com/webview.php"}}
    injectedJavaScript={jsCode}
    javaScriptEnabledAndroid={true}
    style={{height: 100}} />


    That code can open communication between React Native to Webpage inside webview, but i still fail to communication from Web inside webview to React Native. Can anyone explain, how to do it (and is it possible?)?










    share|improve this question



























      0












      0








      0








      I'm new in React Native.



      I need to create communication between RN - Web - RN (RN to Web and then Web back to RN).



      Assume i have list of item inside my web that called inside a webview. Each item inside list can be tapped/clicked. When i click one of item inside item list inside my web, i will redirect it to React Native apps page and show detail of realted item.



      What i have done until now :
      I can inject Javascript from React Native to Webview like this :



      <WebView
      source={{uri: "myweb.com/webview.php"}}
      injectedJavaScript={jsCode}
      javaScriptEnabledAndroid={true}
      style={{height: 100}} />


      That code can open communication between React Native to Webpage inside webview, but i still fail to communication from Web inside webview to React Native. Can anyone explain, how to do it (and is it possible?)?










      share|improve this question
















      I'm new in React Native.



      I need to create communication between RN - Web - RN (RN to Web and then Web back to RN).



      Assume i have list of item inside my web that called inside a webview. Each item inside list can be tapped/clicked. When i click one of item inside item list inside my web, i will redirect it to React Native apps page and show detail of realted item.



      What i have done until now :
      I can inject Javascript from React Native to Webview like this :



      <WebView
      source={{uri: "myweb.com/webview.php"}}
      injectedJavaScript={jsCode}
      javaScriptEnabledAndroid={true}
      style={{height: 100}} />


      That code can open communication between React Native to Webpage inside webview, but i still fail to communication from Web inside webview to React Native. Can anyone explain, how to do it (and is it possible?)?







      javascript react-native webview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 9:57







      Crocodile

















      asked Nov 14 '18 at 9:52









      CrocodileCrocodile

      8710




      8710
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You need to use window.postMessage to communicate from Web to RN.



          In your injectedJavascript do something like this:



          const injectedJs = `
          window.postMessage("Your message");
          `;


          (in your case, you will need to find the list DOM elements and use postMessage on every click)



          And then on your WebView component read this message like so:



          <WebView
          source={{uri: "myweb.com/webview.php"}}
          injectedJavaScript={injectedJs}
          startInLoadingState
          javaScriptEnabledAndroid={true}
          javaScriptEnabled={true}
          onMessage={event => {
          alert('MESSAGE >>>>' + event.nativeEvent.data);
          }}
          />


          You can even send a json string as a message.






          share|improve this answer
























          • So we will use like a "string code" and when React Native receive code from postMessage and then we can navigate to other React Native page ? isn't it ? or we can send navigate instruction directly from Webview ?

            – Crocodile
            Nov 14 '18 at 11:30








          • 1





            You will send a postMessage from WebView and then you can handle all the RN related actions inside your WebView's onMessage function. What we do in our app is send a json string as a message with actionType and bodyData. Then in our onMessage function we have a switch(actionType) and there we handle the different Views that we need to open inside our App.

            – needsleep
            Nov 14 '18 at 12:09











          • i already try this code. This code working fine in iOS, but not in Android. Nothing happen in Android. Can you explain why?

            – Crocodile
            Nov 15 '18 at 3:37











          • When i trace , the problem only appear when you try inject this : const injectedJs = ` window.postMessage("Your message"); `; In Android not working, but working if not come from injected script.

            – Crocodile
            Nov 15 '18 at 4:05











          • I have put together a snack that displays a basic usage of the postMessage, and works on my android phone. It basically triggers an alert with the loaded url. snack.expo.io/B1MDei5aX. I'm sure there are plenty of cases that you would need to tweak this a bit to make it work. For example I noticed that if you make any errors in the injected Javascript then the code doesn't run and there are no errors telling you what is wrong... Maybe you can put together a snack with your example so I can have a look.

            – needsleep
            Nov 15 '18 at 7:33











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297300%2fhow-to-communcation-between-web-inside-webview-back-to-react-native-apps%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You need to use window.postMessage to communicate from Web to RN.



          In your injectedJavascript do something like this:



          const injectedJs = `
          window.postMessage("Your message");
          `;


          (in your case, you will need to find the list DOM elements and use postMessage on every click)



          And then on your WebView component read this message like so:



          <WebView
          source={{uri: "myweb.com/webview.php"}}
          injectedJavaScript={injectedJs}
          startInLoadingState
          javaScriptEnabledAndroid={true}
          javaScriptEnabled={true}
          onMessage={event => {
          alert('MESSAGE >>>>' + event.nativeEvent.data);
          }}
          />


          You can even send a json string as a message.






          share|improve this answer
























          • So we will use like a "string code" and when React Native receive code from postMessage and then we can navigate to other React Native page ? isn't it ? or we can send navigate instruction directly from Webview ?

            – Crocodile
            Nov 14 '18 at 11:30








          • 1





            You will send a postMessage from WebView and then you can handle all the RN related actions inside your WebView's onMessage function. What we do in our app is send a json string as a message with actionType and bodyData. Then in our onMessage function we have a switch(actionType) and there we handle the different Views that we need to open inside our App.

            – needsleep
            Nov 14 '18 at 12:09











          • i already try this code. This code working fine in iOS, but not in Android. Nothing happen in Android. Can you explain why?

            – Crocodile
            Nov 15 '18 at 3:37











          • When i trace , the problem only appear when you try inject this : const injectedJs = ` window.postMessage("Your message"); `; In Android not working, but working if not come from injected script.

            – Crocodile
            Nov 15 '18 at 4:05











          • I have put together a snack that displays a basic usage of the postMessage, and works on my android phone. It basically triggers an alert with the loaded url. snack.expo.io/B1MDei5aX. I'm sure there are plenty of cases that you would need to tweak this a bit to make it work. For example I noticed that if you make any errors in the injected Javascript then the code doesn't run and there are no errors telling you what is wrong... Maybe you can put together a snack with your example so I can have a look.

            – needsleep
            Nov 15 '18 at 7:33
















          1














          You need to use window.postMessage to communicate from Web to RN.



          In your injectedJavascript do something like this:



          const injectedJs = `
          window.postMessage("Your message");
          `;


          (in your case, you will need to find the list DOM elements and use postMessage on every click)



          And then on your WebView component read this message like so:



          <WebView
          source={{uri: "myweb.com/webview.php"}}
          injectedJavaScript={injectedJs}
          startInLoadingState
          javaScriptEnabledAndroid={true}
          javaScriptEnabled={true}
          onMessage={event => {
          alert('MESSAGE >>>>' + event.nativeEvent.data);
          }}
          />


          You can even send a json string as a message.






          share|improve this answer
























          • So we will use like a "string code" and when React Native receive code from postMessage and then we can navigate to other React Native page ? isn't it ? or we can send navigate instruction directly from Webview ?

            – Crocodile
            Nov 14 '18 at 11:30








          • 1





            You will send a postMessage from WebView and then you can handle all the RN related actions inside your WebView's onMessage function. What we do in our app is send a json string as a message with actionType and bodyData. Then in our onMessage function we have a switch(actionType) and there we handle the different Views that we need to open inside our App.

            – needsleep
            Nov 14 '18 at 12:09











          • i already try this code. This code working fine in iOS, but not in Android. Nothing happen in Android. Can you explain why?

            – Crocodile
            Nov 15 '18 at 3:37











          • When i trace , the problem only appear when you try inject this : const injectedJs = ` window.postMessage("Your message"); `; In Android not working, but working if not come from injected script.

            – Crocodile
            Nov 15 '18 at 4:05











          • I have put together a snack that displays a basic usage of the postMessage, and works on my android phone. It basically triggers an alert with the loaded url. snack.expo.io/B1MDei5aX. I'm sure there are plenty of cases that you would need to tweak this a bit to make it work. For example I noticed that if you make any errors in the injected Javascript then the code doesn't run and there are no errors telling you what is wrong... Maybe you can put together a snack with your example so I can have a look.

            – needsleep
            Nov 15 '18 at 7:33














          1












          1








          1







          You need to use window.postMessage to communicate from Web to RN.



          In your injectedJavascript do something like this:



          const injectedJs = `
          window.postMessage("Your message");
          `;


          (in your case, you will need to find the list DOM elements and use postMessage on every click)



          And then on your WebView component read this message like so:



          <WebView
          source={{uri: "myweb.com/webview.php"}}
          injectedJavaScript={injectedJs}
          startInLoadingState
          javaScriptEnabledAndroid={true}
          javaScriptEnabled={true}
          onMessage={event => {
          alert('MESSAGE >>>>' + event.nativeEvent.data);
          }}
          />


          You can even send a json string as a message.






          share|improve this answer













          You need to use window.postMessage to communicate from Web to RN.



          In your injectedJavascript do something like this:



          const injectedJs = `
          window.postMessage("Your message");
          `;


          (in your case, you will need to find the list DOM elements and use postMessage on every click)



          And then on your WebView component read this message like so:



          <WebView
          source={{uri: "myweb.com/webview.php"}}
          injectedJavaScript={injectedJs}
          startInLoadingState
          javaScriptEnabledAndroid={true}
          javaScriptEnabled={true}
          onMessage={event => {
          alert('MESSAGE >>>>' + event.nativeEvent.data);
          }}
          />


          You can even send a json string as a message.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 10:49









          needsleepneedsleep

          1,11639




          1,11639













          • So we will use like a "string code" and when React Native receive code from postMessage and then we can navigate to other React Native page ? isn't it ? or we can send navigate instruction directly from Webview ?

            – Crocodile
            Nov 14 '18 at 11:30








          • 1





            You will send a postMessage from WebView and then you can handle all the RN related actions inside your WebView's onMessage function. What we do in our app is send a json string as a message with actionType and bodyData. Then in our onMessage function we have a switch(actionType) and there we handle the different Views that we need to open inside our App.

            – needsleep
            Nov 14 '18 at 12:09











          • i already try this code. This code working fine in iOS, but not in Android. Nothing happen in Android. Can you explain why?

            – Crocodile
            Nov 15 '18 at 3:37











          • When i trace , the problem only appear when you try inject this : const injectedJs = ` window.postMessage("Your message"); `; In Android not working, but working if not come from injected script.

            – Crocodile
            Nov 15 '18 at 4:05











          • I have put together a snack that displays a basic usage of the postMessage, and works on my android phone. It basically triggers an alert with the loaded url. snack.expo.io/B1MDei5aX. I'm sure there are plenty of cases that you would need to tweak this a bit to make it work. For example I noticed that if you make any errors in the injected Javascript then the code doesn't run and there are no errors telling you what is wrong... Maybe you can put together a snack with your example so I can have a look.

            – needsleep
            Nov 15 '18 at 7:33



















          • So we will use like a "string code" and when React Native receive code from postMessage and then we can navigate to other React Native page ? isn't it ? or we can send navigate instruction directly from Webview ?

            – Crocodile
            Nov 14 '18 at 11:30








          • 1





            You will send a postMessage from WebView and then you can handle all the RN related actions inside your WebView's onMessage function. What we do in our app is send a json string as a message with actionType and bodyData. Then in our onMessage function we have a switch(actionType) and there we handle the different Views that we need to open inside our App.

            – needsleep
            Nov 14 '18 at 12:09











          • i already try this code. This code working fine in iOS, but not in Android. Nothing happen in Android. Can you explain why?

            – Crocodile
            Nov 15 '18 at 3:37











          • When i trace , the problem only appear when you try inject this : const injectedJs = ` window.postMessage("Your message"); `; In Android not working, but working if not come from injected script.

            – Crocodile
            Nov 15 '18 at 4:05











          • I have put together a snack that displays a basic usage of the postMessage, and works on my android phone. It basically triggers an alert with the loaded url. snack.expo.io/B1MDei5aX. I'm sure there are plenty of cases that you would need to tweak this a bit to make it work. For example I noticed that if you make any errors in the injected Javascript then the code doesn't run and there are no errors telling you what is wrong... Maybe you can put together a snack with your example so I can have a look.

            – needsleep
            Nov 15 '18 at 7:33

















          So we will use like a "string code" and when React Native receive code from postMessage and then we can navigate to other React Native page ? isn't it ? or we can send navigate instruction directly from Webview ?

          – Crocodile
          Nov 14 '18 at 11:30







          So we will use like a "string code" and when React Native receive code from postMessage and then we can navigate to other React Native page ? isn't it ? or we can send navigate instruction directly from Webview ?

          – Crocodile
          Nov 14 '18 at 11:30






          1




          1





          You will send a postMessage from WebView and then you can handle all the RN related actions inside your WebView's onMessage function. What we do in our app is send a json string as a message with actionType and bodyData. Then in our onMessage function we have a switch(actionType) and there we handle the different Views that we need to open inside our App.

          – needsleep
          Nov 14 '18 at 12:09





          You will send a postMessage from WebView and then you can handle all the RN related actions inside your WebView's onMessage function. What we do in our app is send a json string as a message with actionType and bodyData. Then in our onMessage function we have a switch(actionType) and there we handle the different Views that we need to open inside our App.

          – needsleep
          Nov 14 '18 at 12:09













          i already try this code. This code working fine in iOS, but not in Android. Nothing happen in Android. Can you explain why?

          – Crocodile
          Nov 15 '18 at 3:37





          i already try this code. This code working fine in iOS, but not in Android. Nothing happen in Android. Can you explain why?

          – Crocodile
          Nov 15 '18 at 3:37













          When i trace , the problem only appear when you try inject this : const injectedJs = ` window.postMessage("Your message"); `; In Android not working, but working if not come from injected script.

          – Crocodile
          Nov 15 '18 at 4:05





          When i trace , the problem only appear when you try inject this : const injectedJs = ` window.postMessage("Your message"); `; In Android not working, but working if not come from injected script.

          – Crocodile
          Nov 15 '18 at 4:05













          I have put together a snack that displays a basic usage of the postMessage, and works on my android phone. It basically triggers an alert with the loaded url. snack.expo.io/B1MDei5aX. I'm sure there are plenty of cases that you would need to tweak this a bit to make it work. For example I noticed that if you make any errors in the injected Javascript then the code doesn't run and there are no errors telling you what is wrong... Maybe you can put together a snack with your example so I can have a look.

          – needsleep
          Nov 15 '18 at 7:33





          I have put together a snack that displays a basic usage of the postMessage, and works on my android phone. It basically triggers an alert with the loaded url. snack.expo.io/B1MDei5aX. I'm sure there are plenty of cases that you would need to tweak this a bit to make it work. For example I noticed that if you make any errors in the injected Javascript then the code doesn't run and there are no errors telling you what is wrong... Maybe you can put together a snack with your example so I can have a look.

          – needsleep
          Nov 15 '18 at 7:33


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297300%2fhow-to-communcation-between-web-inside-webview-back-to-react-native-apps%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          List item for chat from Array inside array React Native

          Thiostrepton

          Jo Brand