iOS:在代码中访问 app-info.plist 变量 [英] iOS: Access app-info.plist variables in code

查看:27
本文介绍了iOS:在代码中访问 app-info.plist 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个通用应用程序 &想在我的代码中访问存储在 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 - 选择打开为),那么您将看到所有可以使用的各种键名.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆