如果主屏幕图标下的应用程序名称是“My Awesome App”,您如何在运行时在应用程序中获取该字符串?
问问题
50128 次
10 回答
139
我会尝试
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
虽然大概您知道自己的应用程序的名称并且可以使用它......</p>
于 2011-11-30T03:54:11.967 回答
30
斯威夫特 3 & 4
Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String ?? ""
Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? ""
斯威夫特 2.2
NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName")
NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleDisplayName")
更多关于“CFBundleName”和“CFBundleDisplayName”的信息
以下来自Apple 关于 Core Foundation Keys 的文档
CFBundleName , “Bundle name”, 包的简称;不打算被用户看到。有关详细信息,请参阅CFBundleName。(推荐,可本地化)
CFBundleDisplayName , “捆绑显示名称”, 捆绑的用户可见名称;由 Siri 使用并且在 iOS 的主屏幕上可见。有关详细信息,请参阅CFBundleDisplayName。(必填,可本地化)
于 2016-09-01T21:23:12.743 回答
19
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
于 2014-12-23T14:40:43.570 回答
14
只是因为我喜欢 Xcode 4.5 获取数组项的新方法。:)
- (NSString*)project_getAppName {
return NSBundle.mainBundle.infoDictionary[@"CFBundleDisplayName"];
}
于 2012-09-29T15:55:35.147 回答
8
对于 Xamarin.iOS 使用:
return ((NSString)NSBundle.MainBundle.InfoDictionary["CFBundleName"]).ToString();
于 2015-01-23T00:23:36.503 回答
8
#include <stdlib.h>
// work for iOS and MacOSX and ~23 times faster than get name from bundle
NSString *app = [NSString stringWithUTF8String:getprogname()];
于 2016-02-10T17:34:22.610 回答
6
斯威夫特 3、4 和 5
let appName = Bundle.main.infoDictionary?["CFBundleName"] as? String
于 2016-11-01T20:37:25.817 回答
5
斯威夫特 3/4
let appName = Bundle.main.object(forInfoDictionaryKey: kCFBundleNameKey as String) as? String
于 2017-07-07T08:51:05.773 回答
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.
于 2011-11-30T03:42:09.380 回答
1
注意:
如果您localizations
在您的应用程序中执行 a,您应该使用 blew 代码来获取真正的本地化显示名称:
Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String ?? ""
Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? ""
而不是:
Bundle.main.object(forInfoDictionaryKey: kCFBundleNameKey as String) as? String
于 2019-04-28T07:53:27.013 回答