iOS:在代码中访问app-info.plist变量 [英] iOS: Access app-info.plist variables in code
问题描述
我正在开发一款Universal app&我希望在我的代码中访问存储在app-info.plist文件中的值。
I am working on a Universal app & would like to access the values stored in app-info.plist file in my code.
原因:我使用以下命令从故事板动态实例化UIViewController:
Reason: I instantiate a UIViewController dynamically from a storyboard using:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
self = [storyboard instantiateViewControllerWithIdentifier:@"ExampleViewController"];
现在,上面的故事板名称@MainStoryboard_iPhone很难看。
Now, having the storyboard name @"MainStoryboard_iPhone" above is ugly.
我想做的事情如下:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:appInfo.mainStoryboardBaseNamePhone bundle:nil];
self = [storyboard instantiateViewControllerWithIdentifier:@"ExampleViewController"];
其中appInfo可能是app-info.plist中所有值的NSDictionary
where appInfo can perhaps be an NSDictionary of all values in app-info.plist
推荐答案
您项目的info.plist中的属性可通过以下方式直接访问...
Attributes from the info.plist for your project are directly accessible by the following...
[[NSBundle mainBundle] objectForInfoDictionaryKey:key_name];
例如,要获取版本号,您可以执行以下操作
For example to get the version number you might do the following
NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
现在有一个问题,版本号现在在info.plist中有两个属性 - 但是你明白了吗?如果您将info.plist视为源代码(右键单击info.plist - 选择Open As),您将看到可以使用的所有各种键名。
There is a gotcha in that the version number now has two attributes in the info.plist - but you get the idea? If you view your info.plist as source code (right click the info.plist - select Open As) then you will get to see all the various key names you can use.
这篇关于iOS:在代码中访问app-info.plist变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!