Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

iphone - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...