How to get the name of the running application in iOS

前端 未结 10 860
误落风尘
误落风尘 2020-12-25 09:28

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?

相关标签:
10条回答
  • 2020-12-25 09:49

    Swift 3, 4, & 5

    let appName = Bundle.main.infoDictionary?["CFBundleName"] as? String
    
    0 讨论(0)
  • 2020-12-25 09:50
    [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
    
    0 讨论(0)
  • 2020-12-25 09:50
    #include <stdlib.h>
    
    // work for iOS and MacOSX and ~23 times faster than get name from bundle
    NSString *app = [NSString stringWithUTF8String:getprogname()];
    
    0 讨论(0)
  • 2020-12-25 09:54

    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)

    0 讨论(0)
  • 2020-12-25 09:54

    Attention:

    If you do a localizations in your app, you should use the blew code to get the true localized display name:

    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
    
    0 讨论(0)
  • 2020-12-25 09:59

    For Xamarin.iOS use:

    return ((NSString)NSBundle.MainBundle.InfoDictionary["CFBundleName"]).ToString();
    
    0 讨论(0)
提交回复
热议问题