Get App Name in Swift

后端 未结 17 1822
被撕碎了的回忆
被撕碎了的回忆 2020-12-24 00:20

How do I get the application name in Swift?

Googling gave me this:

[[[NSBundle mainBundle] infoDictionary] objectForKey:@\"CFBundleName\"];


        
相关标签:
17条回答
  • 2020-12-24 00:49

    This one works perfect for me

    let appName = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleDisplayName") as! String
    
    0 讨论(0)
  • 2020-12-24 00:49

    This is what worked for me in Xcode 11.0 and Swift 5

     let bundleID = Bundle.main.bundleIdentifier
     let bundleInfoDict: NSDictionary = Bundle.main.infoDictionary! as NSDictionary
     let appName = bundleInfoDict["CFBundleName"] as! String
     print(bundleID!)
     print(appName)
    
    0 讨论(0)
  • 2020-12-24 00:52

    // Returns app's name

    public static var appDisplayName: String? {
        if let bundleDisplayName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String {
            return bundleDisplayName
        } else if let bundleName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String {
            return bundleName
        }
        return nil
    }
    
    0 讨论(0)
  • 2020-12-24 00:53

    simple way:

    let appName = NSBundle.mainBundle().infoDictionary?[kCFBundleNameKey as String] as? String
    

    convenient way:

    extension NSBundle {
        class func mainInfoDictionary(key: CFString) -> String? {
            return self.mainBundle().infoDictionary?[key as String] as? String
        }
    }
    print(NSBundle.mainInfoDictionary(kCFBundleNameKey))
    

    kCFBundleNameKey – Standard Info.plist key, see more in CFBundle

    0 讨论(0)
  • 2020-12-24 00:54
    1. All answers that just return CFBundleName will often not return the name the user expects, as if bundles have a CFBundleDisplayName, then this key is displayed by Finder, system frameworks, and most other apps.

    2. Most answers just directly access the info dictionary but info dictionaries can be localized by string files and when accessing them directly, this localization is also ignored and thus again a wrong name may be returned, as Finder will display the localized name.

    3. While CFBundleDisplayName is optional in Info.plist files, CFBundleName actually isn't, but if you forget to add it, nothing will break in your system, so you have a corrupt info dict, yet most users will probably never notice and in that case the code most answers may not return anything meaningful at all.

    Here's my solution (Swift 3):

    private
    func stripFileExtension ( _ filename: String ) -> String {
        var components = filename.components(separatedBy: ".")
        guard components.count > 1 else { return filename }
        components.removeLast()
        return components.joined(separator: ".")
    }
    
    func nameOfApp ( ) -> String {
        let bundle = Bundle.main
        if let name =  bundle.object(forInfoDictionaryKey: "CFBundleDisplayName")
            ?? bundle.object(forInfoDictionaryKey: kCFBundleNameKey as String),
            let stringName = name as? String
            { return stringName }
    
        let bundleURL = bundle.bundleURL
        let filename = bundleURL.lastPathComponent
        return stripFileExtension(filename)
    }
    

    How is this solution better?

    1. It will check CFBundleDisplayName first and only fall back to CFBundleName if not present.

    2. The object() method always operates on the localized version of the info dictionary, so if a localization exists, it will automatically be used.

    3. If neither CFBundleDisplayName nor CFBundleName exist in the dictionary, the code falls back to just using the bundle filename on disk without the extension (so "My Cool App.app" will be "My Cool App"), this is a fallback so that this function will never return nil.

    0 讨论(0)
  • 2020-12-24 00:54

    Try this:

    extension Bundle {
        var displayName: String {
            let name = object(forInfoDictionaryKey: "CFBundleDisplayName") as? String
            return name ?? object(forInfoDictionaryKey: kCFBundleNameKey as String) as! String
        }
    }
    
    0 讨论(0)
提交回复
热议问题