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

前端 未结 30 860
暖寄归人
暖寄归人 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:53

    Simple utility function to return App version as Int

    func getAppVersion() -> Int {
    
            if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
    
                let appVersionClean = appVersion.replacingOccurrences(of: ".", with: "", options: NSString.CompareOptions.literal, range:nil)
    
                if let appVersionNum = Int(appVersionClean) {
                    return appVersionNum
                }
            }
            return 0
        }
    
    0 讨论(0)
  • 2020-12-22 15:54

    For Swift 4.0

    let version = Bundle.main.infoDictionary!["CFBundleShortVersionString"]!
    let build = Bundle.main.infoDictionary!["CFBundleVersion"]!
    
    0 讨论(0)
  • 2020-12-22 15:54

    For Swift 5:

    func getAppCurrentVersionNumber() -> String {
        let nsObject: AnyObject? = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as AnyObject?
        return nsObject as! String
    }
    
    0 讨论(0)
  • 2020-12-22 15:55

    You can now use a constant for this, rather than having to use stringly-typed code like before, which makes things even more convenient.

    var appVersion: String {
        return Bundle.main.infoDictionary![kCFBundleVersionKey as String] as! String
    }
    
    0 讨论(0)
  • 2020-12-22 15:56

    Bundle+Extension.swift (SwiftUI, Swift 5, Xcode 11)

    I combined ideas from a few answers, and extended a bit:

    • a SwiftUI example
    • Displays a warning triangle emoticon (rather than crashing the app) if the key is missing from the Info.plist

    import Foundation

    extension Bundle {
        
        public var appVersionShort: String? {
            if let result = infoDictionary?["CFBundleShortVersionString"] as? String {
                return result
            } else {
                return "⚠️"
            }
        }
        public var appVersionLong: String? {
            if let result = infoDictionary?["CFBundleVersion"] as? String {
                return result
            } else {
                return "⚠️"
            }
        }
        public var appName: String? {
            if let result = infoDictionary?["CFBundleName"] as? String {
                return result
            } else {
                return "⚠️"
            }
        }
    }
    

    SwiftUI example use

    VStack {
    
         Text("Version: \(Bundle.main.appVersionShort!) (\(Bundle.main.appVersionLong!))")
                        .font(.subheadline)
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
    
    0 讨论(0)
  • 2020-12-22 15:57

    I also know this has already been answered but I wrapped up the previous answers:

    (*)Updated for extensions

    extension Bundle {
        var releaseVersionNumber: String? {
            return infoDictionary?["CFBundleShortVersionString"] as? String
        }
        var buildVersionNumber: String? {
            return infoDictionary?["CFBundleVersion"] as? String
        }
        var releaseVersionNumberPretty: String {
            return "v\(releaseVersionNumber ?? "1.0.0")"
        }
    }
    

    Usage:

    someLabel.text = Bundle.main.releaseVersionNumberPretty
    

    @Deprecated: Old answers

    Swift 3.1:

    class func getVersion() -> String {
        guard let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String else {
            return "no version info"
        }
        return version
    }
    

    For older versions:

    class func getVersion() -> String {
        if let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String {
            return version
        }
        return "no version info"
    }
    

    So if you want to set label text or want to use somewhere else;

    self.labelVersion.text = getVersion()
    
    0 讨论(0)
提交回复
热议问题