80

If the application name under the icon on the home screen is "My Awesome App" how do you get that string within the application at runtime?

Jonathan
  • 5,953
  • 1
  • 26
  • 35

10 Answers10

139

I’d try

[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];

although presumably you know your own app’s name and can just use it…

Kjuly
  • 34,476
  • 22
  • 104
  • 118
David Dunham
  • 8,139
  • 3
  • 28
  • 41
  • 5
    This works, although the constant doesn't exist. Guessing it is @"CFBundleDisplayName". I don't want to hard code the app name in as the code I'm writing may get used in other apps. – Jonathan Nov 30 '11 at 23:24
  • Ah, you're right, there's no constant (I copied some existing code that grabbed a different key). The raw string should be fine though. – David Dunham Dec 01 '11 at 17:12
  • 1
    This worked for me after I added the `Bundle display name` property in my `Info.plist` file. – pasql Jan 01 '15 at 15:57
  • 6
    not CFBundleDisplayName but CFBundleName. Do not be deceived – LKM Sep 17 '15 at 13:54
  • 1
    Well it depends. Check the documentation for CFBundleDisplayName. – David Dunham Sep 17 '15 at 17:55
  • Thanks to @pasql, I was missing Bundle display name property in my Info.plist, for this I was getting null from bundle – orchidrudra Feb 15 '21 at 22:23
29

Swift 3 & 4

Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String ?? ""
Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? ""


Swift 2.2

NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName")
NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleDisplayName")


More about 'CFBundleName' and 'CFBundleDisplayName'

The following is from Apple's documentation on Core Foundation Keys

CFBundleName, “Bundle name”, The short name of the bundle; not intended to be seen by the user. See CFBundleName for details. (Recommended, Localizable)

CFBundleDisplayName, “Bundle display name”, The user-visible name of the bundle; used by Siri and visible on the Home screen in iOS. See CFBundleDisplayName for details. (Required, Localizable)

Mobile Dan
  • 6,444
  • 1
  • 44
  • 44
  • 1
    The documentation says that CFBundleDisplayName is a required key… also, it says CFBundleName is the “short name of the bundle; not intended to be seen by the user.” – David Dunham Jun 15 '17 at 15:20
  • Thank you @DavidDunham! I updated the text in my answer under "More about 'CFBundleName' and 'CFBundleDisplayName'" to match Apple's current documentation. – Mobile Dan Jun 15 '17 at 16:26
  • Curiosity: What's the point of using this code, if it might be nil and a default value should be provided? Why not just use that value? –  Nov 20 '18 at 04:10
19
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
14

Just because I love the Xcode 4.5 new way to get an array item. :)

- (NSString*)project_getAppName {
    return NSBundle.mainBundle.infoDictionary[@"CFBundleDisplayName"];
}
Mr Rogers
  • 6,091
  • 2
  • 32
  • 34
Franck
  • 8,939
  • 8
  • 39
  • 57
8

For Xamarin.iOS use:

return ((NSString)NSBundle.MainBundle.InfoDictionary["CFBundleName"]).ToString();
Kraig McConaghy
  • 258
  • 4
  • 4
8
#include <stdlib.h>

// work for iOS and MacOSX and ~23 times faster than get name from bundle
NSString *app = [NSString stringWithUTF8String:getprogname()];
Roman Solodyashkin
  • 799
  • 12
  • 17
  • Useful too if you're writing a command-line tool that isn't in a bundle structure or doesn't have an Info.plist file to populate infoDictionary. – user2067021 Aug 31 '17 at 11:54
6

Swift 3, 4, & 5

let appName = Bundle.main.infoDictionary?["CFBundleName"] as? String
M-P
  • 4,909
  • 3
  • 25
  • 31
5

Swift 3/4

let appName =  Bundle.main.object(forInfoDictionaryKey: kCFBundleNameKey as String) as? String
Jess
  • 1,394
  • 12
  • 12
3
NSString* applicationName = [entry objectForKey:(id)kCGWindowOwnerName];

Here is a good post with examples of what you are looking for. The OP didn't accept anything, which is unfortunate, but the answers are useful.

Community
  • 1
  • 1
Billy Coover
  • 3,827
  • 5
  • 36
  • 50
1

Attention:

If you do a localizations in your app, you should use the blew code to get the true localized display name:

Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String ?? ""
Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? ""

rather than:

Bundle.main.object(forInfoDictionaryKey: kCFBundleNameKey as String) as? String
yuanjilee
  • 429
  • 5
  • 16