iOS:在代码中访问 app-info.plist 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9530075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: Access app-info.plist variables in code
提问by sherlock
I am working on a Universal app & would like to access the values stored in app-info.plist file in my code.
我正在开发一个通用应用程序,并希望在我的代码中访问存储在 app-info.plist 文件中的值。
Reason: I instantiate a UIViewController dynamically from a storyboard using:
原因:我使用以下方法从故事板动态实例化 UIViewController:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
self = [storyboard instantiateViewControllerWithIdentifier:@"ExampleViewController"];
Now, having the storyboard name @"MainStoryboard_iPhone" above is ugly.
现在,上面的故事板名称@"MainStoryboard_iPhone" 是丑陋的。
I want to do something like:
我想做类似的事情:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:appInfo.mainStoryboardBaseNamePhone bundle:nil];
self = [storyboard instantiateViewControllerWithIdentifier:@"ExampleViewController"];
where appInfo can perhaps be an NSDictionary of all values in app-info.plist
其中 appInfo 可能是 app-info.plist 中所有值的 NSDictionary
回答by Damo
Attributes from the info.plist for your project are directly accessible by the following...
您项目的 info.plist 中的属性可以通过以下方式直接访问...
[[NSBundle mainBundle] objectForInfoDictionaryKey:key_name];
For example to get the version number you might do the following
例如要获取版本号,您可以执行以下操作
NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
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.
有一个问题,版本号现在在 info.plist 中有两个属性 - 但你明白了吗?如果您将 info.plist 视为源代码(右键单击 info.plist - 选择打开为),那么您将看到所有可以使用的各种键名。
回答by Maverick
Swift 4+syntax for Damo's solution
Damo 解决方案的Swift 4+语法
Bundle.main.object(forInfoDictionaryKey: "KEY_NAME")
Example
例子
let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion")
回答by rckoenes
Well you can acces the info.plist very easly :
那么你可以很容易地访问 info.plist :
Getting the name of the storyboard:
获取故事板的名称:
NSString *storyboard = [[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"];
Not sure wether it will detect if it is in a iPad and it should use the UIMainStoryboardFile~ipad
key instated.
不确定它是否会检测到它是否在 iPad 中,是否应该使用UIMainStoryboardFile~ipad
设置的密钥。
回答by James Lobo
NSString *path = [[NSBundle mainBundle] pathForResource: @"YOURPLISTNAME" ofType: @"plist"];
NSMutableDictionary *dictplist =[[NSMutableDictionary alloc] initWithContentsOfFile:path];
回答by svth
You can also use the infoDictionary method on NSBundle:
你也可以在 NSBundle 上使用 infoDictionary 方法:
NSDictionary *infoPlistDict = [[NSBundle mainBundle] infoDictionary];
NSString *version = infoPlistDict[@"CFBundleVersion"];