Determine if running inside a React Native WebView

Viewed 4839

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 !

3 Answers

You can also do something like this

injectedJavaScript="window.isRNWebView=true"

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

Related