iOS: Access app-info.plist variables in code
Asked Answered
W

5

115

I am working on a Universal app & would like to access the values stored in app-info.plist file in my code.

Reason: I instantiate a UIViewController dynamically from a storyboard using:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
self = [storyboard instantiateViewControllerWithIdentifier:@"ExampleViewController"];

Now, having the storyboard name @"MainStoryboard_iPhone" above is ugly.

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

Weevily answered 2/3, 2012 at 8:18 Comment(0)
E
263

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"];

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.

Escalate answered 2/3, 2012 at 8:24 Comment(4)
As mentioned by @Answerbot in this post:https://mcmap.net/q/189571/-read-version-from-info-plist : "CFBundleVersion has been repurposed to be Build and Version is CFBundleShortVersionString", so now for retrieving the version number from plist, we need to use: NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];Overstay
for 11.3 it doesn't work, because NSBundle was renamed and the new Bundle has it not included yet. Do xou have a Update?Region
If I have an Enterprise iOS app that is distributed via the web, can I add key/value pairs in the PLIST file, and subsequently read those values from the app at the time of download?Izzy
how we can update value of CFBundleVersion from the AppDelegate.m?Unnatural
A
36

Swift 4+ syntax for Damo's solution

Bundle.main.object(forInfoDictionaryKey: "KEY_NAME")

Example

let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion")
Afoul answered 1/11, 2017 at 8:3 Comment(1)
For beginners like me, Bundle requires import SystemConfigurationRiana
C
12

Well you can acces the info.plist very easly :

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.

Clara answered 2/3, 2012 at 8:26 Comment(1)
XCode automatically populates the key 'UIMainStoryboardFile' with the iPhone / iPad Storyboard files, depending on the device :)Weevily
D
9
NSString *path = [[NSBundle mainBundle] pathForResource: @"YOURPLISTNAME" ofType: @"plist"]; 
NSMutableDictionary *dictplist =[[NSMutableDictionary alloc] initWithContentsOfFile:path];
Deepseated answered 7/2, 2013 at 12:6 Comment(1)
You can also use NSDictionary: NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];Flo
G
6

You can also use the infoDictionary method on NSBundle:

NSDictionary *infoPlistDict = [[NSBundle mainBundle] infoDictionary];
NSString *version = infoPlistDict[@"CFBundleVersion"];
Goodale answered 23/11, 2015 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.