ChatGPT解决这个技术问题 Extra ChatGPT

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


D
Damo

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.


As mentioned by @Answerbot in this post: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"];
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?
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?
how we can update value of CFBundleVersion from the AppDelegate.m?
M
Maverick

Swift 4+ syntax for Damo's solution

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

Example

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

For beginners like me, Bundle requires import SystemConfiguration
r
rckoenes

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.


XCode automatically populates the key 'UIMainStoryboardFile' with the iPhone / iPad Storyboard files, depending on the device :)
S
Stunner
NSString *path = [[NSBundle mainBundle] pathForResource: @"YOURPLISTNAME" ofType: @"plist"]; 
NSMutableDictionary *dictplist =[[NSMutableDictionary alloc] initWithContentsOfFile:path];

You can also use NSDictionary: NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
s
svth

You can also use the infoDictionary method on NSBundle:

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