Search code examples
react-nativewebviewpostmessage

window.postMessage doesn't send the data to OnMessage WebView


I have created a webview in my react-native component then i wanted to use postMessage to send data and read it through OnMessage . I don't know actually if the problem was in my postMessage or in my onMessage option of the webView . Can you tell me where is the problem please ? Here is my app Component :

      let Script1 = `
      var data = {
      message : "OnFocusEvent"
        };
      window.postMessage(JSON.stringify({data}),"*");
         `;
     export default class App extends Component {
      constructor(props) {
      super(props);}
      render() {
       return (
       <View style={styles.container}>

      <RNEnhanceWebview
      ref={ref => (this.webView = ref)}
      style={{ flex: 1 }}
      source={{
        html:
          ' </br></br></br></br><form> <input id="input" class="input" 
      type="text" placeholder="name"/></form>'
      }}
      keyboardDisplayRequiresUserAction={false} //ios
      autoFocus={true} //android
      injectedJavaScript={Script1}
      automaticallyAdjustContentInsets={false}
      allowFileAccessFromFileURLs={true}
      scalesPageToFit={false}
      mixedContentMode={"always"}
      javaScriptEnabled={true}
      javaScriptEnabledAndroid={true}
      startInLoadingState={true}

      onMessage={event => {
        console.log(event.nativeEvent.data);
        console.log(JSON.parse(event.nativeEvent.data));
      }}
      onLoad={() => {}}
    />
      </View>
       }

Do you have any suggestion to show my message data ?


Solution

  • You are right, it seems that your code doesn't work on Android but works fine on iOs...

    This is a strange one, it looks as if the postMessage doesn't work on the first page load, but if you call it with a delay, like onFocus it works as expected.

    For example try this script:

    let Script1 = `
        document.getElementById("input").addEventListener("focus", function() {  
          var data = {
              type: "OnFocusEvent",
              message : "OnFocusEvent"
          };
          window.postMessage(JSON.stringify({data}),"*");
        });
    
        document.getElementById("input").addEventListener("blur", function() {  
          var data = {
              type: "OnBlurEvent",
              message : "OnBlurEvent"
          };
          window.postMessage(JSON.stringify({data}),"*");
        });
    `;
    
    onMessage={event => {
        console.log(event.nativeEvent.data);
        console.log(JSON.parse(event.nativeEvent.data));
        const messageData = JSON.parse(event.nativeEvent.data);
        const messageType = messageData.type;
    
        switch(messageType) {
            case "OnFocusEvent":
             console.log("Do whatever you want onFocus")
             break;
            case "OnBlurEvent":
             console.log("Do whatever you want onBlur")
             break;
            default:
             console.log("do nothing");
             break;
        }
    }}
    

    Here's the example on snack: https://snack.expo.io/rkANOd9lN

    Hope it helps :)