Get App Name in Swift

后端 未结 17 1823
被撕碎了的回忆
被撕碎了的回忆 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:55

    For swift 5, iOS 13*

    As mentioned before, it‘s an optional, so put it in a guard statement. I do this using a struct:

    struct Model {
        struct ProgVariablen{
            static var appBundleName:String {
                get {guard Bundle.main.infoDictionary != nil else {return ""}
                    return Bundle.main.infoDictionary!["CFBundleName"] as! String
                }//end get
            }//end computed property
            static var appBundleShortVersion:String {
                get {guard Bundle.main.infoDictionary != nil else {return ""}
                    return Bundle.main.infoDictionary ["CFBundleShortVersionString"] as! String
                }//end get
            }//end computed property
            static var appBundleBuild:String {
                get {guard Bundle.main.infoDictionary != nil else {return ""}
                    return Bundle.main.infoDictionary["CFBundleVersion"] as! String
            }//end get
        }//end computed property
    
        //initialsieren der Variablen
        init(
             appBundleName:String,
             appBundleShortVersion:String,
             appBundleBuild:String,
             )
        {
            // do here nothing for 'let'
            // do here nothing for 'computed properties'
            // your other ‘var’ are here like:
            // ProgVariablen.var1 = var1
        }//end init
        }//end struct ProgVariablen
    }//end struct Model
    

    Usage:

    print("Model.ProgVariablen.appBundleName: '\(Model.ProgVariablen.appBundleName)'")
    
    0 讨论(0)
  • 2020-12-24 00:57

    Same answer in Swift 4.2

    extension Bundle {
        static func appName() -> String {
            guard let dictionary = Bundle.main.infoDictionary else {
                return ""
            }
            if let version : String = dictionary["CFBundleName"] as? String {
                return version
            } else {
                return ""
            }
        }
    }
    

    you can use it like below

    let appName = Bundle.appName()
    

    Hope this helps :)

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

    This one works for me in Swift 4.2

    guard let dictionary = Bundle.main.infoDictionary else { return "" }
    if let version: String = dictionary["CFBundleDisplayName"] as? String {
       return version
    } else {
       return ""
    }
    
    0 讨论(0)
  • 2020-12-24 01:01

    I have created a simple extension to get the app name that is shown under the icon on the Home screen.

    By default, apps only have CFBundleName set. Some apps, however, set CFBundleDisplayName (The user-visible name of the bundle) to change the title under the app icon. Adding spaces is often the case, e.g. bundle name "ExampleApp" could have bundle display name set to "Example App".

    extension Bundle {
        // Name of the app - title under the icon.
        var displayName: String? {
                return object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ??
                    object(forInfoDictionaryKey: "CFBundleName") as? String
        }
    }
    

    Usage:

    let appName = Bundle.main.displayName
    
    0 讨论(0)
  • 2020-12-24 01:06

    I believe this solution is more elegant. What's more, using object(forInfoDictionaryKey:) is encouraged by Apple:

    "Use of this method is preferred over other access methods because it returns the localized value of a key when one is available."

    extension Bundle {
        var displayName: String? {
            return object(forInfoDictionaryKey: "CFBundleDisplayName") as? String
        }
    }
    

    Accessing bundle display name:

    if let displayName = Bundle.main.displayName {
        print(displayName)
    }
    
    0 讨论(0)
提交回复
热议问题