Closed gfaraj closed 2 years ago
I managed to get it to work by modifying the above code to this:
var payloadStr = JSON.stringify(payload);
if (window.ReactNativeWebView) {
window.ReactNativeWebView.postMessage(payloadStr);
} else if (window.parent) {
window.parent.postMessage(payloadStr, '*');
} else {
window.postMessage(payloadStr, '*');
}
It works on both native and web. I think adding something about this to the documentation would be great. Thanks for making this!
I managed to get it to work by modifying the above code to this:
var payloadStr = JSON.stringify(payload); if (window.ReactNativeWebView) { window.ReactNativeWebView.postMessage(payloadStr); } else if (window.parent) { window.parent.postMessage(payloadStr, '*'); } else { window.postMessage(payloadStr, '*'); }
It works on both native and web. I think adding something about this to the documentation would be great. Thanks for making this!
I had the same problem and your solution worked. Thanks!
I'm using this package to bring feature parity to my web and native apps, but I can't seem to communicate back to react native from the website.
My usage is:
The website I'm loading within the webview/iframe does this:
The native webview works fine through window.ReactNativeWebView.postMessage but for web window.ReactNativeWebView is not defined and so I had to figure out where to post the message. This still doesn't trigger my onMessage though.
Any ideas?