[ACCEPTED]-Getting an iPhone app's product name at runtime?-iphone

Accepted answer
Score: 141

Try this

NSBundle *bundle = [NSBundle mainBundle];
NSDictionary *info = [bundle infoDictionary];
NSString *prodName = [info objectForKey:@"CFBundleDisplayName"];

0

Score: 55

Good answers here. I would add one thing 5 though.

Rather than using @"CFBundleDisplayName", which 4 has the potential to change in future, it's 3 best to cast the string constant supplied 2 in CFBundle.h like so:

[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey];

Thus future-proofing 1 your code.

Score: 43

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

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

Here's an example 1 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
Score: 14

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

CFBundleDisplayName = "My Localized App Name";

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

In that case I used 1 localizedInfoDirectory like below.

NSDictionary *locinfo = [bundle localizedInfoDictionary];
Score: 9

You can use the direct approach,

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

0

Score: 6

For completeness, Swift 3.0 would be;

let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String

0

Score: 2

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

let productName = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String
Score: 1

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

var prodName = NSBundle.MainBundle.InfoDictionary.ObjectForKey(new NSString("CFBundleDisplayName")) as NSString;
Score: 1

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

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

If you need your app 3 to have a different name from the bundle 2 and may change Info.plist, you could do 1 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
Score: 0

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

let productName = NSBundle.mainBundle().infoDictionary!["CFBundleName"]!
Score: 0

The following code would be better.

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

0

Score: 0
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

More Related questions