How do I get the App version and build number using Swift?

前端 未结 30 852
暖寄归人
暖寄归人 2020-12-22 15:21

I have an IOS app with an Azure back-end, and would like to log certain events, like logins and which versions of the app users are running.

How can I return the ver

相关标签:
30条回答
  • 2020-12-22 15:36
    public var appVersionNumberString: String {
        get {
            return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
        }
    }
    
    0 讨论(0)
  • 2020-12-22 15:37

    I made an Extension on Bundle

    extension Bundle {
    
        var appName: String {
            return infoDictionary?["CFBundleName"] as! String
        }
    
        var bundleId: String {
            return bundleIdentifier!
        }
    
        var versionNumber: String {
            return infoDictionary?["CFBundleShortVersionString"] as! String 
        }
    
        var buildNumber: String {
            return infoDictionary?["CFBundleVersion"] as! String
        }
    
    }
    

    and then use it

    versionLabel.text = "\(Bundle.main.appName) v \(Bundle.main.versionNumber) (Build \(Bundle.main.buildNumber))"
    
    0 讨论(0)
  • 2020-12-22 15:37

    Swift 5 as UIApplication extension

    extension UIApplication {
        static var release: String {
            return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String? ?? "x.x"
        }
        static var build: String {
            return Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String? ?? "x"
        }
        static var version: String {
            return "\(release).\(build)"
        }
    }
    

    example use:

    print("release: \(UIApplication.release)")
    print("build: \(UIApplication.build)")
    print("version: \(UIApplication.version)")
    
    0 讨论(0)
  • 2020-12-22 15:37

    for anyone interested, there's a nice and neat library called SwifterSwift available at github and also fully documented for every version of swift (see swifterswift.com).

    using this library, reading app version and build number would be as easy as this:

    import SwifterSwift
    
    let buildNumber = SwifterSwift.appBuild
    let version = SwifterSwift.appVersion
    
    0 讨论(0)
  • 2020-12-22 15:38

    For Swift 3.0 NSBundle doesn't work, Following code works perfectly.

    let versionNumberString =
          Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString")
              as! String
    

    and for just the build number, it is:

    let buildNumberString =
          Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion")
              as! String
    

    Confusingly 'CFBundleVersion' is the build number as entered in Xcode on General->Identity.

    0 讨论(0)
  • 2020-12-22 15:39

    EDIT

    Updated for Swift 4.2

    let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
    

    EDIT

    As pointed out by @azdev on the new version of Xcode you will get a compile error for trying my previous solution, to solve this just edit it as suggested to unwrap the bundle dictionary using a !

    let nsObject: AnyObject? = Bundle.main.infoDictionary!["CFBundleShortVersionString"]
    

    End Edit

    Just use the same logic than in Objective-C but with some small changes

    //First get the nsObject by defining as an optional anyObject
    let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary["CFBundleShortVersionString"]
    
    //Then just cast the object as a String, but be careful, you may want to double check for nil
    let version = nsObject as! String
    

    I hope this helps you out.

    David

    0 讨论(0)
提交回复
热议问题