在您的React Native应用中获取Xcode版本号

如果您出于任何原因希望能够在应用程序中显示版本号,我将向您展示如何在React Native iOS应用程序中实现该版本号。

步骤1:

使用react-native init versionApp创建一个新的react native项目

第2步:

编辑AppDelegate.m文件并添加以下代码行

  //创建版本变量并创建props对象 
  NSString * version = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@“ CFBundleShortVersionString”]; 
  NSDictionary * initialProps = @ {@“ version”:version}; 

第三步

将创建的props对象传递到同一AppDelegate.m文件中的initialProperties中。 默认情况下,为零。

之前

  RCTRootView * rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation 
  moduleName:@“ VersionApp” 
  initialProperties:nil //更改此行并传入新的props对象 
  launchOptions:launchOptions]; 

之后

  RCTRootView * rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation 
  moduleName:@“ VersionApp” 
  initialProperties:props 
  launchOptions:launchOptions]; 

步骤4:

与通常使用this.props.version一样,打开index.js文件并访问传递的道具。

注意:您必须重新生成该应用程序。 请记住,实时重新加载仅适用于JS代码更改,不适用于本机代码更改。