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

sherlock
  • 1,521
  • 2
  • 13
  • 17

5 Answers5

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.

Damo
  • 12,840
  • 3
  • 51
  • 62
  • 5
    As mentioned by @Answerbot in this post:http://stackoverflow.com/a/4059118/1210822 : "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"]; – sonoshin Oct 30 '12 at 14:38
  • 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? – Yvonne Marggraf Aug 15 '18 at 14:00
  • 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? – Brant Nov 08 '19 at 19:24
  • how we can update value of CFBundleVersion from the AppDelegate.m? – Pragnesh Jun 24 '20 at 09:45
36

Swift 4+ syntax for Damo's solution

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

Example

let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion")
Maverick
  • 3,209
  • 1
  • 34
  • 40
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.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • 6
    XCode automatically populates the key 'UIMainStoryboardFile' with the iPhone / iPad Storyboard files, depending on the device :) – sherlock Mar 16 '12 at 10:19
9
NSString *path = [[NSBundle mainBundle] pathForResource: @"YOURPLISTNAME" ofType: @"plist"]; 
NSMutableDictionary *dictplist =[[NSMutableDictionary alloc] initWithContentsOfFile:path];
Stunner
  • 12,025
  • 12
  • 86
  • 145
James Lobo
  • 317
  • 1
  • 4
  • 11
6

You can also use the infoDictionary method on NSBundle:

NSDictionary *infoPlistDict = [[NSBundle mainBundle] infoDictionary];
NSString *version = infoPlistDict[@"CFBundleVersion"];
svth
  • 1,303
  • 1
  • 14
  • 23