iOS: Access app-info.plist variables in code

IphoneObjective CIosIpadinfo.plist

Iphone Problem Overview


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

Iphone Solutions


Solution 1 - Iphone

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.

Solution 2 - Iphone

Swift 4+ syntax for Damo's solution

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

Example

let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion")

Solution 3 - Iphone

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.

Solution 4 - Iphone

NSString *path = [[NSBundle mainBundle] pathForResource: @"YOURPLISTNAME" ofType: @"plist"]; 
NSMutableDictionary *dictplist =[[NSMutableDictionary alloc] initWithContentsOfFile:path];

Solution 5 - Iphone

You can also use the infoDictionary method on NSBundle:

NSDictionary *infoPlistDict = [[NSBundle mainBundle] infoDictionary];
NSString *version = infoPlistDict[@"CFBundleVersion"];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsherlockView Question on Stackoverflow
Solution 1 - IphoneDamoView Answer on Stackoverflow
Solution 2 - IphoneMaverickView Answer on Stackoverflow
Solution 3 - IphonerckoenesView Answer on Stackoverflow
Solution 4 - IphoneJames LoboView Answer on Stackoverflow
Solution 5 - IphonesvthView Answer on Stackoverflow