iOS: Access app-info.plist variables in code

前端 未结 5 1490
-上瘾入骨i
-上瘾入骨i 2020-11-28 23:21

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

相关标签:
5条回答
  • 2020-11-28 23:52

    Swift 4+ syntax for Damo's solution

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

    Example

    let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion")
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 00:14
    NSString *path = [[NSBundle mainBundle] pathForResource: @"YOURPLISTNAME" ofType: @"plist"]; 
    NSMutableDictionary *dictplist =[[NSMutableDictionary alloc] initWithContentsOfFile:path];
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 00:15

    You can also use the infoDictionary method on NSBundle:

    NSDictionary *infoPlistDict = [[NSBundle mainBundle] infoDictionary];
    NSString *version = infoPlistDict[@"CFBundleVersion"];
    
    0 讨论(0)
提交回复
热议问题