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?
10 Answers
I’d try
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
although presumably you know your own app’s name and can just use it…

- 34,476
- 22
- 104
- 118

- 8,139
- 3
- 28
- 41
-
5This 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
-
1This worked for me after I added the `Bundle display name` property in my `Info.plist` file. – pasql Jan 01 '15 at 15:57
-
6not CFBundleDisplayName but CFBundleName. Do not be deceived – LKM Sep 17 '15 at 13:54
-
1Well 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
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)

- 6,444
- 1
- 44
- 44
-
1The 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
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
Just because I love the Xcode 4.5 new way to get an array item. :)
- (NSString*)project_getAppName {
return NSBundle.mainBundle.infoDictionary[@"CFBundleDisplayName"];
}
For Xamarin.iOS use:
return ((NSString)NSBundle.MainBundle.InfoDictionary["CFBundleName"]).ToString();

- 258
- 4
- 4
#include <stdlib.h>
// work for iOS and MacOSX and ~23 times faster than get name from bundle
NSString *app = [NSString stringWithUTF8String:getprogname()];

- 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
Swift 3, 4, & 5
let appName = Bundle.main.infoDictionary?["CFBundleName"] as? String

- 4,909
- 3
- 25
- 31
Swift 3/4
let appName = Bundle.main.object(forInfoDictionaryKey: kCFBundleNameKey as String) as? String

- 1,394
- 12
- 12
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.

- 1
- 1

- 3,827
- 5
- 36
- 50
-
Thanks, some great information in that post. Prefer the one liner in Davids answer though ;) +1 – Jonathan Nov 30 '11 at 23:27
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

- 429
- 5
- 16