-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
window.ReactNativeWebView is undefined on Android platform, iOS works just fine. #1829
Comments
I created an example repo where this can be reproduced. In iOS, an alert shows up because the website is call callback function. Also, the codesandbox with the web-view behaviour. cc: @jamonholmgren |
I met the same issue, did you have any good solution? |
Thank you @fpena for providing the sample. I have been scratching my head for days but no clue until I discovered another issue which cause this to happen. Apparently the <WebView
useWebKit={true}
allowsInlineMediaPlayback={true}
javaScriptEnabledAndroid={true}
javaScriptEnabled={true}
originWhitelist={['*']}
source={{ uri: fullLink }}
bounces={ false }
pagingEnabled= { true }
domStorageEnabled={ true }
style={{marginTop: notch_margin}}
injectedJavaScriptBeforeContentLoaded={injectBeforeLoad}
onNavigationStateChange={(event)=>{
if(event.url.includes('foobar')){
navigation.navigate(someOtherScreen);
}
}}
onMessage={(event) => {
//receive message
}}
/> |
@joe06102 Somehow my issue "disappeared" once I figure out the prop |
@Scarle-t the repo I created doesn't have |
@fpena I do aware that the issue may not related to |
I'm going to have a look, thanks for the repro @fpena |
@fpena I'm not sure I can reproduce with your example. When I inspect the page I can see in the console that window.onload has failed due to callback being undefind, however window.ReactNativeWebView is defined. To fix and display the alert I just changed your injected JS to window.callback = function() {} and your onload to call the function on the window. EDIT: Oh also your onload is running potentially before the JS was injected. But there are workaround for that. Let me see how I can share that with you |
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
same issue |
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
Meeting the same problem. Is there a working solution? |
I encounter this today, luckily I remember I can use it yesterday morning, so I did a git bysect, and found I have this, if I comment the - const modifiedContent = content.replace('</body>', '<script>console.log("loaded")</script></body>');
+ const modifiedContent = content //.replace('</body>', '<script>console.log("loaded")</script></body>'); this wait, I'm using And this only happened after rebuild (by pressing |
This doesn't even work after I do some refactor. So I think this bug needs to be reopened. |
race condition! |
Make sure onMessage is never undefined by doing this: <WebView
originWhitelist={['*']}
source={{ html: wikiHTMLString }}
onMessage={onMessageReference.current ?? ((message) => {
console.log('WebView onMessage (before onMessageReference.current ready)', message);
})}
ref={webViewReference}
injectedJavaScriptBeforeContentLoaded={preloadScript}
// Open chrome://inspect/#devices to debug the WebView
webvie because react-native-webview/docs/Guide.md Line 424 in dd31719
I don't think this is a good design, because nowadays some callback are provided by async hooks, for example https://github.com/linonetwo/react-native-postmessage-cat 's So what about But this doc is the fact that really make me realize this react-native-webview/docs/Guide.md Line 424 in dd31719
|
Bug description:
window.ReactNativeWebView
is undefined on Android platform, iOS works just fine.To Reproduce:
Build for both platforms from same project, open Chrome's DevTool and Safari's Web Inspector, type
window.ReactNativeWebView
in the Console section and send it.Expected behavior:
Webpage JS can call
window.ReactNativeWebView.postMessage()
on both platform successfully.Further action handled within React Native.
Screenshots/Videos:
Console at the top showing iOS console, bottom is Android console
![Image showing iOS console at the top, android console at the bottom](https://camo.githubusercontent.com/aea4dce729c65a06e48555be933b8128b5b0a324d4a488c751d6bf4c3ef1b063/68747470733a2f2f692e696d6775722e636f6d2f746639596d46592e706e67)
Environment:
OS: iOS
OS version: 14.3
react-native version: 0.63.4
react-native-webview version: 11.0.2
OS: Android
OS version: 11
react-native version: 0.63.4
react-native-webview version: 11.0.2
The text was updated successfully, but these errors were encountered: