在Obj-C iOS应用程序中,我可以使用#if (TARGET_IPHONE_SIMULATOR)来编写仅限模拟器的代码.

在react native中,我可以使用:

if (__DEV__) {
 .. do something special
}

检测开发模式.

我们可以使用Platform.OS === 'ios'来检测平台(Android/iOS).

但我们如何检测应用程序是否在模拟器中运行?

我问这个问题的原因是,我的应用程序使用摄像头扫描条形码,而iOS模拟器不支持这一点.

推荐答案

你可以很容易地用react-native-device-info做到这一点,比如:

import DeviceInfo from 'react-native-device-info'

isSimulator() {
  // https://github.com/react-native-community/react-native-device-info#isemulator
  return DeviceInfo.isEmulator();
},

React-native相关问答推荐

如何在Reaction Native中将身体分成三部分

在 React Native 中打开后日期 Select 器不关闭

在 ReactNative 中调用 RCTDeviceEventEmitter.emit 时出错

使用 React Native 正确处理注销

应用程序主体尚未注册

如何在 react native 中以 redux 形式设置隐藏字段?

调用 ES6 方法时绑定上下文.如何从称为回调的方法中访问对象?

如何在 React Native 的嵌套 Touchable 中传播touch 事件?

React Native Card Carousel 视图?

用于部署后设置的 React 配置文件

React-Native 应用程序的 Jest 测试 Animated.View

redux-observable Promise 在单元测试中没有得到解决

react-native:如何在离线模式下在模拟器上运行应用程序?

React Native Remote Debugger 在 Chrome 中显示缓存的包

React Native:ScrollView 中的 TouchableOpacity onPress 问题

当组件在react-native 中重新呈现时,动态不透明度不会改变

大型列表的 React-Native FlatList 性能问题

在react-native中调试应用程序崩溃

Mapping children of a parent component

react-native 开始给出 Invalid 正则表达式无效错误