3

I'm currently developing a RN app using the WebView component showing a remote URI, and in the JS loaded through this URI, I would like to be able to determine if I'm inside a RN WebView or not.

Have someone already faced this issue ? Can someone help me ?

Thanks a lot !

rlebosse
  • 81
  • 1
  • 5
  • try this library: https://github.com/faisalman/ua-parser-js check for webview on android and webkit for iOS – maxi-code Sep 09 '19 at 22:17

3 Answers3

4

You can also do something like this

injectedJavaScript="window.isRNWebView=true"

lfkwtz
  • 1,007
  • 2
  • 11
  • 25
  • You may also want to add `injectedJavaScriptBeforeContentLoaded` too. Somehow it didn't inject the JS code on the first load. – ozgrozer Dec 15 '22 at 16:07
3

React Native creates a bridge to the WebView and in order to do that it overrides the value of window.postMessage.

They save the original implementation under window.originalPostMessage.

Thus you can rely on the existence of window.originalPostMessage to detect that you're in React Native.

The Android code that does this is here and on iOS it's here

RangerMauve
  • 308
  • 1
  • 2
  • 8
0

React Native does not do anything else other than instantiating a native WebView and setting up a communication channel to JavaScript. That means, any native solution to the problem also applies. See the following resources:

martinarroyo
  • 9,389
  • 3
  • 38
  • 75