react-native-webview: injectedJavaScript doesnt seem to work on IOS
Bug description: Setup the WebView and try to use the “injectedJavaScript” prop. It works as expected on Android but when on IOS it does nothing.
To Reproduce:
- Use this code below as your render method:
render() {
const runFirst = `
document.body.style.backgroundColor = 'red';
setTimeout(function() { window.alert('hi') }, 2000);
true; // note: this is required, or you'll sometimes get silent failures
`
return (
<View style={{ flex: 1 }}>
<WebView
source={{
uri: 'https://github.com/react-native-community/react-native-webview',
}}
injectedJavaScript={runFirst}
/>
</View>
)
}
Expected behavior: Expected to render the github page for react-native-webview with a red background on both platforms.
Screenshots/Videos:
Android:

IOS:

Environment:
- OS: IOS
- OS version: 13.3
- react-native version: 0.62
- react-native-webview version: 9.0.2
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 10
- Comments: 15 (5 by maintainers)
According to the guide, you now must pass a function as the
onMessageprop for injected Javascript to work. But, it will work in Android without it anyway.I am already using ‘onMessage’. Still it is not working to inject javascript. My WebView something like that: <WebView onLoadEnd={() => { this._setstatus() }} onNavigationStateChange={this.on_navigaton_new} injectedJavaScript={custom_script} onMessage={x => this.onMessageGet(x)} javaScriptEnabled={true} source={{ uri: this.state.url, forceReload: this.state.forceReload }} allowsLinkPsreview={true} incognito={true} domStorageEnabled={true} ref={(ref) => this.myWebView = ref}/>
@PrantikMondal - I had the same issue, the JS didnt inject itself into the webview unless i injected it with the help of ref
export default class App extends Component { render() { const run = ‘document.body.style.backgroundColor = ‘blue’;true;’;
} }
Thanks @vikbos, good solution for this issue.
Here is my code.
Don’t add property
enableApplePay.I was able to get this to work with functional components
Without the ref, for some reason injectedJavaScript will not run. With the ref set as
webViewRef.current.injectJavaScript(runFirstScript);, injectedJavaScript will run as well.Could we close this issue now that I’ve created the more specific issue #1311 (which addresses the root cause) to track it instead?
Did not see this issue until just now, but this should be resolved in: https://github.com/react-native-community/react-native-webview/pull/1286
which is now in the latest release!
injectedJavaScriptBeforeContentLoad for Android is implemented in this PR; just need a maintainer to get some time to merge it: https://github.com/react-native-community/react-native-webview/pull/1099
Don’t get too excited though, as it’s a bit broken due to this issue, which may be far harder to solve: https://github.com/react-native-community/react-native-webview/issues/1250
This is not a regression; it’s long been the case that you’ve needed to pass onMessage in order for JS injection to work (and I’ve never liked it). It’s also actually documented, but admittedly hard to find.
Here’s my line-by-line explanation of how to fix it, in case anyone feels like implementing a PR: https://github.com/react-native-community/react-native-webview/issues/1260#issuecomment-607148843