Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Access app-info.plist variables in code

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

like image 811
sherlock Avatar asked Mar 02 '12 08:03

sherlock


People also ask

How do I open Info plist as a source code?

Press ⌘+⇧+o to get the Quick Open Dialog. Enter name of property list file e.g. " info. plist " and press "Enter" Open "Version Editor" -> Property List File is shown as source code.

How do I access plist?

In Xcode you can see a plist's XML structure by right-clicking on a . plist file, and choosing Open As → Source Code. If you look closely, you can spot the different values and structures in the XML.

How do I edit Info plist files?

All you have to do is click on the blue project icon -on top of the left- and go to the “Build Settings” tab, search “Info. plist” and choose “Info. plist File” section. Then change the information of the section from your folder's name.


1 Answers

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.

like image 138
Damo Avatar answered Nov 10 '22 01:11

Damo