Skip to content

Instantly share code, notes, and snippets.

@mikelehen
Created May 18, 2015 22:39
Show Gist options
  • Save mikelehen/822a82a37bf30431a104 to your computer and use it in GitHub Desktop.
Save mikelehen/822a82a37bf30431a104 to your computer and use it in GitHub Desktop.
15:25 < mikelehen> Is there a way to detect that you're in the react native environment (e.g. by looking for the existence of a global variable or examine the navigator.userAgent)? I work on a 3rd-party library and we'd like to track usage within react native apps.
15:26 < mikelehen> I thought about just doing require('react-native') in a try/catch block to see if it works, but that breaks tools (like browserify) that try to statically resolve all require()s they find.
15:26 <@vjeux> mikelehen: this is a very interesting question
15:26 <@vjeux> mikelehen: i don't think there's any good answer right now
15:27 <@vjeux> mikelehen: do you have a suggestion on how you would like it working?
15:27 < mikelehen> vjeux: Not sure. Maybe react could introcude a ReactVersion global or something.
15:28 < mikelehen> (i.e. on the window object)
15:28 <@vjeux> right now we have require('react-native').Platform.OS === 'ios'/'android'
15:29 <@vjeux> what are you going to do based on this information?
15:29 < mikelehen> Just record it in our analytics.
15:29 < ReactifluxSlack> <lwansbrough> @vjeux maybe you should polyfill navigator.platform?
15:29 <@vjeux> navigator.platform
15:29 <@vjeux> "MacIntel"
15:30 <@vjeux> i've got this on chrome
15:30 <@vjeux> appCodeName: "Mozilla"
15:30 <@vjeux> appName: "Netscape"
15:30 <@vjeux> appVersion: "5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36"
15:30 < ReactifluxSlack> <lwansbrough> i suppose overwriting it would be a bad call
15:30 <@vjeux> product: "Gecko"
15:30 <@vjeux> all of this is pretty crazy town
15:30 < mikelehen> Yeah. Sticking something anywhere in there would be sufficient for me, but I don't know if it's going to break some silly assumption of some code somewhere.
15:30 < ReactifluxSlack> <lwansbrough> `product` might be a good place to put `ReactNative` or something
15:31 <@vjeux> but yeah, we could do navigator.platform = 'ReactNative'
15:31 < ReactifluxSlack> <lwansbrough> @vjeux Probably dont want to overwrite current platform information
15:31 < ReactifluxSlack> <lwansbrough> might be useful when we have Android support
15:32 <@vjeux> so product='ReactNative' and platform='iOS'
15:33 < mikelehen> That'd work great for my usage.
15:33 <@vjeux> mikelehen: cool, do you want to submit a pull request? :)
15:33 < ReactifluxSlack> <lwansbrough> might be good to use the values that are exposed by the phones in their native browsers
15:33 <@vjeux> https://github.com/facebook/react-native/blob/0fbe0913042e314345f6a033a3681372c741466b/Libraries/JavaScriptAppEngine/Initialization/InitializeJavaScriptAppEngine.js#L114
15:33 < ReactifluxSlack> <lwansbrough> that way checks are consistent between web and react native
15:35 < ReactifluxSlack> <lwansbrough> https://i.imgur.com/NPuT5ka.png
15:35 < mikelehen> vjeux: Haha. Well played. =] I probably can't do it right now, but I can log an issue and then maybe send a PR in a week or two when I have time to poke at it.
15:35 < ReactifluxSlack> <lwansbrough> iOS says "iPhone"
15:35 <@vjeux> mikelehen: yeah, having an issue to talk about it would be great
15:35 <@vjeux> @lwansbrough: nice, someone asked how to detect iphone vs ipad this morning
15:35 <@vjeux> would be a nice place as well
15:36 < ReactifluxSlack> <lwansbrough> yeah definitely
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment