Getting an iPhone app's product name at runtime?

前端 未结 12 2073
醉梦人生
醉梦人生 2021-01-30 19:43

How can this be achieved? I would like to get the name so i can display it within an app, without having to change it in code each time i change a name, of course.

相关标签:
12条回答
  • 2021-01-30 20:09

    Good answers here. I would add one thing though.

    Rather than using @"CFBundleDisplayName", which has the potential to change in future, it's best to cast the string constant supplied in CFBundle.h like so:

    [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey];
    

    Thus future-proofing your code.

    0 讨论(0)
  • 2021-01-30 20:11

    You can use the direct approach,

    NSString* appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
    
    0 讨论(0)
  • 2021-01-30 20:12

    Try this

    NSBundle *bundle = [NSBundle mainBundle];
    NSDictionary *info = [bundle infoDictionary];
    NSString *prodName = [info objectForKey:@"CFBundleDisplayName"];
    
    0 讨论(0)
  • 2021-01-30 20:12
    let productName =  Bundle.main.infoDictionary?["CFBundleName"] as? String
    

    let displayName =  Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String
    

    0 讨论(0)
  • 2021-01-30 20:22

    Here is the Xamarin.iOS version of @epatel's answer:

    var prodName = NSBundle.MainBundle.InfoDictionary.ObjectForKey(new NSString("CFBundleDisplayName")) as NSString;
    
    0 讨论(0)
  • 2021-01-30 20:23

    You could have all bundle details from this dictionary "info". print this dictionary and get what you want.

    NSBundle *bundle = [NSBundle mainBundle];
    NSDictionary *info = [bundle infoDictionary];
    
    0 讨论(0)
提交回复
热议问题