37
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]

this API returns Bundle Display Name in plist.

However my app is localized and has different display name.

so i need to get localized display name in InfoPlist.strings that should vary with device language setting.

Krunal
  • 77,632
  • 48
  • 245
  • 261
Umgre
  • 667
  • 1
  • 6
  • 15

6 Answers6

71

Have you tried -[NSBundle localizedInfoDictionary]?

[[[NSBundle mainBundle] localizedInfoDictionary]
       objectForKey:@"CFBundleDisplayName"]
justin
  • 104,054
  • 14
  • 179
  • 226
  • @Umgre you're welcome. there are a ton of APIs -- lots to memorize =) – justin Feb 06 '12 at 05:59
  • 15
    This method more directly returns a localized string too: `[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]` – artur Jul 22 '14 at 20:18
6

Try this for Swift:

// Localized
if let displayName = Bundle.main.localizedInfoDictionary?["CFBundleDisplayName"] as? String {
    print("App Display Name - \(displayName)")
}

// Non-Localized
if let displayName = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String {
    print("App Display Name - \(displayName)")
}

Also try this, if you have not set Display Name

// Localized
if let appName = Bundle.main. localizedInfoDictionary?["CFBundleName"] as? String {
    print("App Name - \(appName)")
}

// Non-Localized
if let appName = Bundle.main.infoDictionary?["CFBundleName"] as? String {
    print("App Name - \(appName)")
}

Useful trick:

// Print bundle info dictionary to get complete details about app
print("Bundle.main.infoDictionary - \(Bundle.main.infoDictionary)")
print("Bundle.main.localizedInfoDictionary - \(Bundle.main.localizedInfoDictionary)")
shim
  • 9,289
  • 12
  • 69
  • 108
Krunal
  • 77,632
  • 48
  • 245
  • 261
4

I suggest to use kCFBundleNameKey:

[[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString*)kCFBundleNameKey]
Pang
  • 9,564
  • 146
  • 81
  • 122
Igor
  • 12,165
  • 4
  • 57
  • 73
2
Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String ?? ""

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

Simon
  • 438
  • 4
  • 14
1
class Utils {
    static var localizedAppName: String? {
        return Bundle.main.localizedInfoDictionary?["CFBundleDisplayName"] as? String
    }
}
Nico S.
  • 3,056
  • 1
  • 30
  • 64
Dmitry Isaev
  • 3,888
  • 2
  • 37
  • 49
1

This works fine in Swift 5:

extension Bundle {
    var displayName: String? {
        return Bundle.main.infoDictionary?["CFBundleName"] as? String
    }
}
  
if let displayName = Bundle.main.displayName {
    print("displayName : \(displayName)")
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
deepak
  • 78
  • 4