如何知道是否在本机反应的设备或模拟器上运行

Ria*_*ias 6 environment device ios-simulator react-native

为了区分 React Native 中的开发模式和生产模式,定义了常量__DEV__,当 Debug = True 时。

是否定义了类似的常量,可以让我在代码中知道代码是在设备上还是在模拟器中运行?

我还能从哪里获得此类信息。

G. *_*ide 6

您可以使用这个,然后只需执行以下操作:

console.log(DeviceInfo.getModel()); // it returns 'Simulator'
Run Code Online (Sandbox Code Playgroud)


rob*_*408 5

自从 G. Hamaide 的答案发布后,DeviceInfo 包添加了方法isEmulator

DeviceInfo.isEmulator()