Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native: How can I detect if my code is running in the Simulator?

In a Obj-C iOS app I can use #if (TARGET_IPHONE_SIMULATOR) to write simulator-only code.

In react native I can use:

if (__DEV__) {  .. do something special } 

.. to detect development mode.

We can use Platform.OS === 'ios' to detect the platform (Android/iOS). See here for more info Platform Docs

But how do we detect if the app is running in the simulator?

The reason I ask is that my app uses the camera to scan barcodes, and this isn't supported in the iOS Simulator.

like image 721
Ben Clayton Avatar asked Jan 11 '16 17:01

Ben Clayton


People also ask

How do I know if my device is an emulator in React Native?

React Native device info provide isEmulator() function to check app running in emulator or not it will return only true or false . isEmulator() function work in ios, android, and windows.


1 Answers

You can do this pretty easily with react-native-device-info, like so:

import DeviceInfo from 'react-native-device-info'  isSimulator() {   // https://github.com/react-native-community/react-native-device-info#isemulator   return DeviceInfo.isEmulator(); }, 
like image 155
Lane Rettig Avatar answered Sep 28 '22 02:09

Lane Rettig