ios 在运行时获取 iPhone 应用程序的产品名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1247142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting an iPhone app's product name at runtime?
提问by Edward An
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.
如何做到这一点?我想获得名称,以便我可以在应用程序中显示它,而不必每次更改名称时都在代码中更改它,当然。
回答by epatel
Try this
尝试这个
NSBundle *bundle = [NSBundle mainBundle];
NSDictionary *info = [bundle infoDictionary];
NSString *prodName = [info objectForKey:@"CFBundleDisplayName"];
回答by imnk
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:
与其使用@"CFBundleDisplayName"(它在未来可能会改变),不如像这样转换 CFBundle.h 中提供的字符串常量:
[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey];
Thus future-proofing your code.
从而使您的代码面向未来。
回答by guptron
According to Apple, using - objectForInfoDictionaryKey:
directly on the NSBundle
object is preferred:
根据 Apple 的说法,- objectForInfoDictionaryKey:
直接在NSBundle
对象上使用是首选:
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:
这是 Swift 中的一个示例:
let appName = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as! String
// Or use key "CFBundleDisplayName"
Update for Swift 3 - thanks Jef.
Swift 3 的更新 - 感谢 Jef。
let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String
回答by tokentoken
I had a problem when I localize my application name by using InfoPlist.strings, like
我在使用 InfoPlist.strings 本地化应用程序名称时遇到了问题,例如
CFBundleDisplayName = "My Localized App Name";
I could not obtain localized application name if I use infoDictionary.
如果使用 infoDictionary,则无法获得本地化的应用程序名称。
In that case I used localizedInfoDirectory like below.
在那种情况下,我使用了如下所示的 localizedInfoDirectory。
NSDictionary *locinfo = [bundle localizedInfoDictionary];
回答by karim
You can use the direct approach,
你可以使用直接的方法,
NSString* appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
回答by Patrick
For completeness, Swift 3.0 would be;
为完整起见,Swift 3.0 将是;
let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
回答by Dan Loewenherz
Here's the cleanest approach I could come up with using Swift 3:
这是我使用 Swift 3 能想到的最简洁的方法:
let productName = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String
回答by richdcs
Here is the Xamarin.iOS version of @epatel's answer:
这是@epatel 答案的 Xamarin.iOS 版本:
var prodName = NSBundle.MainBundle.InfoDictionary.ObjectForKey(new NSString("CFBundleDisplayName")) as NSString;
回答by RajeshKumar R
回答by alones
The following code would be better.
下面的代码会更好。
NSBundle *bundle = [NSBundle mainBundle];
NSDictionary *info = [bundle infoDictionary];
self.appName = [info objectForKey:@"CFBundleExecutable"];