如何在React Native iOS应用程序中运行时读取Info.Plist?

9

运行时,有没有一种方法可以从 Info.Plist 中读取 CFBundleVersion?

我想在应用程序的“关于”框中显示版本信息。

提前感谢,

-Ed

1个回答

16

我建议你使用这个非常完美的

我想你会得到所有需要的信息。

编辑

你还可以在AppDelegate.m文件中使用一些Obj-C语言。只需添加这几行:

NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

NSDictionary *props = @{@"version" : version};

替换这些代码:

RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"NeedlIOS"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];

使用这些:

RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"NeedlIOS"
                                               initialProperties:props
                                                   launchOptions:launchOptions];

你需要将版本作为属性传递给你的首个iOS视图。这意味着在您的index.ios.js文件中,您可以使用this.props.version获取应用程序版本。


1
谢谢,react-native-device-info 在几分钟内解决了问题!也感谢您关于如何从 AppDelegate.m 传递 props 到 react-native 的提示。我期待这个提示将会很有用!https://github.com/rebeccahughes/react-native-device-info - Ed of the Mountain
3
为什么只有直接从 Xcode 运行才有效?run-ios 命令无法正常工作。 - jose920405
1
我可以确认,在React Native 0.48.4上,iOS的run-ios没有传递initialProperties。我也不知道为什么! - Ben Clayton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接