How to get the name of the running application in iOS

五迷三道 提交于 2019-11-30 02:33:04
David Dunham

I’d try

[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];

although presumably you know your own app’s name and can just use it…

Swift 3 & 4

Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String ?? ""
Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? ""


Swift 2.2

NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName")
NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleDisplayName")


More about 'CFBundleName' and 'CFBundleDisplayName'

The following is from Apple's documentation on Core Foundation Keys

CFBundleName, “Bundle name”, The short name of the bundle; not intended to be seen by the user. See CFBundleName for details. (Recommended, Localizable)

CFBundleDisplayName, “Bundle display name”, The user-visible name of the bundle; used by Siri and visible on the Home screen in iOS. See CFBundleDisplayName for details. (Required, Localizable)

[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
Franck

Just because I love the Xcode 4.5 new way to get an array item. :)

- (NSString*)project_getAppName {
    return NSBundle.mainBundle.infoDictionary[@"CFBundleDisplayName"];
}

For Xamarin.iOS use:

return ((NSString)NSBundle.MainBundle.InfoDictionary["CFBundleName"]).ToString();
#include <stdlib.h>

// work for iOS and MacOSX and ~23 times faster than get name from bundle
NSString *app = [NSString stringWithUTF8String:getprogname()];

Swift 3/4

let appName =  Bundle.main.object(forInfoDictionaryKey: kCFBundleNameKey as String) as? String
Billy Coover
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.

Swift 3

let appName = Bundle.main.infoDictionary?["CFBundleName"] as? String

Attention:

If you local your app name in different language, you should use the blow code to get the true localized display name:

Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String ?? ""
Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? ""

ranther than the blow:

Bundle.main.object(forInfoDictionaryKey: kCFBundleNameKey as String) as? String
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!