question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

injectedJavaScript doesnt seem to work on IOS

See original GitHub issue

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:

  1. 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: Screenshot_1585924185

IOS: Simulator Screen Shot - iPhone 8 - 2020-04-03 at 11 30 34

Environment:

  • OS: IOS
  • OS version: 13.3
  • react-native version: 0.62
  • react-native-webview version: 9.0.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

37reactions
tdcookcommented, Apr 4, 2020

According to the guide, you now must pass a function as the onMessage prop for injected Javascript to work. But, it will work in Android without it anyway.

8reactions
PrantikMondalcommented, Aug 4, 2020

According to the guide, you now must pass a function as the onMessage prop 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}/>

Read more comments on GitHub >

github_iconTop Results From Across the Web

injectedJavaScript is not working in Webview of react native
Well, you have more than one issue here. First one is your web view needs to be wrapped in a containing View component...
Read more >
Need help with react-native Webview, injecting react ... - Reddit
The string interpolation doesn't work. What should I do if I'm trying to pass data from react native into the javascript string that's...
Read more >
why react native webview injectedJavascript doesn't work on ...
Coding example for the question why react native webview injectedJavascript doesn't work on IOS?-React Native.
Read more >
Injecting JavaScript Into Web View In iOS - Swift Senpai
Learn how to inject JavaScript into the iOS web view (WKWebView) to customize the HTML styling to fit the look and feel of...
Read more >
react-native-webview - npm
React Native WebView component for iOS, Android, macOS, and Windows. ... for "RNCWebView does not exist" it likely means you forgot to run...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found