React Native WebView onMessage doesn't do anything

后端 未结 4 894
-上瘾入骨i
-上瘾入骨i 2020-12-18 22:17

I\'m trying to use the onMessage listener. The website is executing a postMessage (window.postMessage(\"Post message from web\");) but react native\'s webview o

4条回答
  •  一向
    一向 (楼主)
    2020-12-18 22:57

    According to this issue, you need to wait until the React Native postMessage has replaced the native window.postMessage (don’t ask me why they are replacing a native function instead of creating a new one).

    One solution is to do something like:

    function waitForBridge() {
    
       //the react native postMessage has only 1 parameter
       //while the default one has 2, so check the signature
       //of the function
    
       if (window.postMessage.length !== 1){
         setTimeout(waitForBridge, 200);
       }
       else {
         window.postMessage('abc');
       }
    }
    
    window.onload = waitForBridge;
    

提交回复
热议问题