Search code examples
react-nativeexporeact-native-web

Find out OS via react-native-web


I have already searched the whole internet for the question in the title, but have not found anything. I am using react native web with expo and trying to find out which operating system the users of a web application are using.

I came across the Platform.OS function, but this function does NOT solve the problem I just described. Platform.OS returns 'web' for both Android and IOS.

btw I'm using: 
react-native-version: 0.64.1
expo version: 40.0.0
react-native-web version: 0.16.2

Solution

  • The main purpose of Platform.OS is to distinguish between devices (like iOS, android and web).

    You might want to look at react-native-device-info : https://github.com/react-native-device-info/react-native-device-info

    There is a lot of methods there to get some informations on the device that is running your app. A lot of methods are not supported on the web, and you have a table on the documentation explaining what is supported or not for each device.

    You could use getBaseOs() though, for example on my RN web project it would return Mac OS, you won't get the full OS version with this though.

    You could also use getUseAgent() which would give you somethings like this: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 (but this will be different for each browser).