74

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.

13 Answers 13

143

Try this

NSBundle *bundle = [NSBundle mainBundle];
NSDictionary *info = [bundle infoDictionary];
NSString *prodName = [info objectForKey:@"CFBundleDisplayName"];
4
  • 24
    CFBundleDisplayName returns the Bundle Display Name. OP asked for the Product Name. For this use CFBundleName. In most cases the two will be the same, but may be different if you set a new display name (for something displayed under the icon which is not suitable as a filename). Commented Oct 28, 2011 at 10:39
  • 4
    Apple discusses this in a technical article.. Of particular note is that there's no constant for CFBundleDisplayName -- a string literal is the right thing to use. Commented Nov 21, 2012 at 20:00
  • 2
    @ChrisNewman is incorrect: CFBundleName is absolutely NOT the Product Name (you can check this by changing one but not the other in your xcode settings). They default to being the same. (I discovered this the hard way).
    – Adam
    Commented Dec 1, 2014 at 10:35
  • Since my comment was made obsolete somewhere between 1 and 3 SDK versions ago, what is the current correct answer to this question? Commented Dec 1, 2014 at 15:00
57

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.

1
  • 8
    This is a different key and might not be the display name in all cases. Surprisingly, a string literal is what Apple recommends for this particular case. Commented Nov 21, 2012 at 20:01
45

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
3
  • Come on people... this is the quality answer.
    – Jonny
    Commented Nov 18, 2015 at 9:28
  • has changed significantly for swift3 - let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String
    – Jef
    Commented Mar 20, 2017 at 12:48
  • Could use the constant kCFBundleNameKey instead of a simple string: kCFBundleNameKey as String
    – Matt Mc
    Commented Apr 4, 2018 at 21:40
14

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];
9

You can use the direct approach,

NSString* appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
6

For completeness, Swift 3.0 would be;

let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
2
  • Why vote down with no comment? The code works and the answer isn't a duplicate :/
    – Patrick
    Commented Sep 5, 2016 at 10:13
  • 1
    This is actually not answering the question which is asking for the Product Name which can be set differently than the Bundle Display Name.
    – awolf
    Commented Feb 6, 2017 at 17:27
2

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

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

A simple way is as follows. Be aware that this returns the name of your app's bundle, which you can change to be different from your app's product name.

// (Swift 5)
static let bundleName = Bundle.main.object(forInfoDictionaryKey: kCFBundleNameKey as String) as! String

If you need your app to have a different name from the bundle and may change Info.plist, you could do something like the following:

// (Swift 5)
// To use this, include a key and value in your app's Info.plist file:
// Key: ProductName
// Value: $(PRODUCT_NAME)
// By default PRODUCT_NAME is the same as your project build target name, $(TARGET_NAME), but this may be changed.
// If you do so, you may wish to change the CFBundleName value to $(TARGET_NAME) in the Info.plist file.
// PRODUCT_NAME is defined in the target's Build Settings in the Packaging section.
static let productName = Bundle.main.object(forInfoDictionaryKey: "ProductName") as! String
1

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

var prodName = NSBundle.MainBundle.InfoDictionary.ObjectForKey(new NSString("CFBundleDisplayName")) as NSString;
1
let productName =  Bundle.main.infoDictionary?["CFBundleName"] as? String

enter image description here

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

enter image description here

0

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

The following code would be better.

NSBundle *bundle = [NSBundle mainBundle];
NSDictionary *info = [bundle infoDictionary];
self.appName = [info objectForKey:@"CFBundleExecutable"];
-2

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];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.