ios 如何在iOS中获取正在运行的应用程序的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8320860/
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
How to get the name of the running application in iOS
提问by Jonathan
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?
如果主屏幕上图标下的应用程序名称是“My Awesome App”,您如何在运行时在应用程序中获取该字符串?
回答by David Dunham
I'd try
我会尝试
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
although presumably you know your own app's name and can just use it…
虽然大概您知道自己的应用程序的名称并且可以使用它......
回答by Mobile Dan
Swift 3 & 4
斯威夫特 3 & 4
Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String ?? ""
Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? ""
Swift 2.2
斯威夫特 2.2
NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName")
NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleDisplayName")
More about 'CFBundleName' and 'CFBundleDisplayName'
关于“CFBundleName”和“CFBundleDisplayName”的更多信息
The following is from Apple's documentation on Core Foundation Keys
以下来自Apple 关于 Core Foundation Keys 的文档
CFBundleName, “Bundle name”, The short name of the bundle; not intended to be seen by the user. See CFBundleNamefor details. (Recommended, Localizable)
CFBundleName, “捆绑包名称”,捆绑包的简称;不打算被用户看到。有关详细信息,请参阅CFBundleName。(推荐,可本地化)
CFBundleDisplayName, “Bundle display name”, The user-visible name of the bundle; used by Siri and visible on the Home screen in iOS. See CFBundleDisplayNamefor details. (Required, Localizable)
CFBundleDisplayName,“捆绑显示名称”,捆绑的用户可见名称;由 Siri 使用并在 iOS 的主屏幕上可见。有关详细信息,请参阅CFBundleDisplayName。(必需,可本地化)
回答by Mobile Dan
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
回答by Franck
Just because I love the Xcode 4.5 new way to get an array item. :)
只是因为我喜欢 Xcode 4.5 获取数组项的新方法。:)
- (NSString*)project_getAppName {
return NSBundle.mainBundle.infoDictionary[@"CFBundleDisplayName"];
}
回答by Kraig McConaghy
For Xamarin.iOS use:
对于 Xamarin.iOS 使用:
return ((NSString)NSBundle.MainBundle.InfoDictionary["CFBundleName"]).ToString();
回答by Roman Solodyashkin
#include <stdlib.h>
// work for iOS and MacOSX and ~23 times faster than get name from bundle
NSString *app = [NSString stringWithUTF8String:getprogname()];
回答by Jess
Swift 3/4
斯威夫特 3/4
let appName = Bundle.main.object(forInfoDictionaryKey: kCFBundleNameKey as String) as? String
回答by mpatzer
Swift 3, 4, & 5
斯威夫特 3、4 和 5
let appName = Bundle.main.infoDictionary?["CFBundleName"] as? String
回答by Billy Coover
NSString* applicationName = [entry objectForKey:(id)kCGWindowOwnerName];
Here is a good postwith examples of what you are looking for. The OP didn't accept anything, which is unfortunate, but the answers are useful.
这是一篇很好的帖子,其中包含您正在寻找的内容的示例。OP 不接受任何内容,这很不幸,但答案很有用。
回答by yuanjilee
Attention:
注意:
If you do a localizations
in your app, you should use the blew code to get the true localized display name:
如果您localizations
在应用程序中执行 a ,则应使用 blew 代码来获取真正的本地化显示名称:
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