Android window.ReactNativeWebView.postMessage issue
See original GitHub issueHi there,
We implemented version 5.0.5 of the library with #243 incorporated into it using window.ReactNativeWebView.postMessage for bidirectional communication. We use both iOS and Android with “react-native”: “0.57.0”.
The problem we see is the following:
iOS => web view => ✅ (use injectJavaScript to send a message)
web view => iOS => ✅ (use window.ReactNativeWebView.postMessage to send a message)
Android => web view => ✅ (use injectJavaScript to send a message)
web view => Android => 👎 (use window.ReactNativeWebView.postMessage to send a message)
Android errors with:
java bridge method can't be invoked on a non injected object.
This feels somewhat weird, since the whole java bridge is supposed to be managed for us, as long as messagingEnabled=true, which is computed from messagingEnabled={typeof this.props.onMessage === 'function'}. Our WebView has onMessage implemented with a function, so that all should be good, and is also “confirmed” by the fact that iOS works as expected.
Am I missing something obvious, or is this a bug?
Thanks in advance!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:23
Top Related StackOverflow Question
@Titozzz I didn’t realize that the
targetOriginargument must be omitted inwindow.ReactNativeWebView.postMessage().If the
targetOrigin(*) is added, it will throw themethod does not exist error!I just had the same error and narrowed it down in my case to a javascript binding issue. You can easily reproduce the
java bridge method can't be invoked on a non injected objecterror by assigning thewindow.ReactNativeWebView.postMessageto a local variable then trying to invoke that:By assigning to a local variable and calling that, javascript is changing the functions
thisand the Android WebView appears to be quite unhappy with that. It is quite easy to work around by ensuring the binding is preserved:As a real world example and the reason I came across this was for some code which was shared between hosting in a WebView or an iframe and I was trying to do this: