大佬教程收集整理的这篇文章主要介绍了android – React Native:如何检测我的代码是否在模拟器中运行?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在反应本地我可以使用:
if (__DEV__) { .. do something special }
检测开发模式.
我们可以使用Platform.OS ===’ios’来检测平台(Android / iOS).
查看更多信息Platform Docs
但是如何检测应用程序是否在模拟器中运行?
我问的原因是我的应用程序使用相机扫描条形码,这在iOS模拟器中不受支持.
在您的AppDelegate中,RCTRootView初始化的地方,您会检查它是否像在常规iOS应用程序中一样;然后将此信息传递给反应根视图作为其initialProperties:
BOOL isSimulator = NO; #if target_ipHONE_SIMULATOR isSimulator = YES; #endif RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLOCATIOn modulename:@"ReactDemo" initialProperties:@{@"isSimulator": @(isSimulator)} launchOptions:launchOptions];
现在您可以通过您的反应组件的道具在JavaScript中访问它:
this.props.isSimulator
在Android上,在您的MainActivity中,扩展了ReactActivity,您可以使用类似的方法:
public Boolean isEmulator() { return Build.FINGERPRINT.startsWith("generic") || Build.FINGERPRINT.startsWith("unkNown") || Build.MODEl.contains("google_sdk") || Build.MODEl.contains("Emulator") || Build.MODEl.contains("Android SDK built for x86") || Build.MANUFACTURER.contains("Genymotion") || (Build.bRAND.startsWith("generic") && Build.DEVICE.startsWith("generic")) || "google_sdk".equals(Build.PRODUCT); } @Override protected Bundle getLaunchOptions() { Bundle opts = new Bundle(); opts.putBoolean("isEmulator",isEmulator()); return opts; }
以上是大佬教程为你收集整理的android – React Native:如何检测我的代码是否在模拟器中运行?全部内容,希望文章能够帮你解决android – React Native:如何检测我的代码是否在模拟器中运行?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。