Getting an iPhone app's product name at runtime?

前端 未结 12 2072
醉梦人生
醉梦人生 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:03

    For completeness, Swift 3.0 would be;

    let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
    
    0 讨论(0)
  • 2021-01-30 20:05

    I had a problem when I localize my application name by using InfoPlist.strings, like

    CFBundleDisplayName = "My Localized App Name";
    

    I could not obtain localized application name if I use infoDictionary.

    In that case I used localizedInfoDirectory like below.

    NSDictionary *locinfo = [bundle localizedInfoDictionary];
    
    0 讨论(0)
  • 2021-01-30 20:05

    The following code would be better.

    NSBundle *bundle = [NSBundle mainBundle];
    NSDictionary *info = [bundle infoDictionary];
    self.appName = [info objectForKey:@"CFBundleExecutable"];
    
    0 讨论(0)
  • 2021-01-30 20:06

    This is just a swift update for this very old question. I was in need of swift answer and it was kind of tricky(Unwrapping optionals) in swift syntax so sharing it here

    let productName = NSBundle.mainBundle().infoDictionary!["CFBundleName"]!
    
    0 讨论(0)
  • 2021-01-30 20:08

    According to Apple, using - objectForInfoDictionaryKey: directly on the NSBundle object is preferred:

    Use of this method is preferred over other access methods because it returns the localized value of a key when one is available.

    Here's an example in Swift:

    let appName = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as! String
    // Or use key "CFBundleDisplayName"
    

    Update for Swift 3 - thanks Jef.

    let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String
    
    0 讨论(0)
  • 2021-01-30 20:08

    Here's the cleanest approach I could come up with using Swift 3:

    let productName = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String
    
    0 讨论(0)
提交回复
热议问题