Program Tip

Swift로 버전 및 빌드 정보 얻기

programtip 2020. 10. 8. 18:49
반응형

Swift로 버전 및 빌드 정보 얻기


버전 및 빌드 정보를 검색하기 위해 Main NSBundle에 대한 액세스 권한을 얻으려고합니다. 문제는 신속하게 시도하고 싶습니다 .Objective-C에서 Build.text = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];. 그러나 나는 신속하게 시작해야 할 곳을 모르고 새로운 구문으로 작성하려고 시도했습니다.


Swift 구문에 무엇이 잘못 되었습니까? 이것은 작동하는 것 같습니다.

if let text = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
    print(text)
}

Swift 3/4 버전

func version() -> String {
    let dictionary = Bundle.main.infoDictionary!
    let version = dictionary["CFBundleShortVersionString"] as! String
    let build = dictionary["CFBundleVersion"] as! String
    return "\(version) build \(build)"
} 

Swift 2.x 버전

func version() -> String {
    let dictionary = NSBundle.mainBundle().infoDictionary!
    let version = dictionary["CFBundleShortVersionString"] as String
    let build = dictionary["CFBundleVersion"] as String
    return "\(version) build \(build)"
}

여기 에서 볼 수 있습니다 .


Xcode 6의 최종 릴리스를 위해

NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as? String

"?" 여기서 infoDictionary 뒤의 문자가 중요합니다.


AppName, AppVersionBuildNumber...에 대한 신속한 방법

if let dict = NSBundle.mainBundle().infoDictionary {
   if let version = dict["CFBundleShortVersionString"] as? String,
       let bundleVersion = dict["CFBundleVersion"] as? String,
       let appName = dict["CFBundleName"] as? String {
           return "You're using \(appName) v\(version) (Build \(bundleVersion))."
   }
}

다음은 빌드 및 버전을 얻는 간단한 방법입니다.

Swift 4.X의 경우

 if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
     print(version)
   }

 if let build = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
     print(build)
   }

Objective C의 경우

NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

NSString * currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

문제가 있으면 알려주세요. 이것은 나를 위해 일하고 있습니다.


신속하게 UIApplication의 확장으로 다음과 같이 만들 것입니다.

extension UIApplication {

    func applicationVersion() -> String {

        return NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
    }

    func applicationBuild() -> String {

        return NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleVersionKey as String) as! String
    }

    func versionBuild() -> String {

        let version = self.applicationVersion()
        let build = self.applicationBuild()

        return "v\(version)(\(build))"
    }
}

Then you can just use following to get everything you need:

let version = UIApplication.sharedApplication.applicationVersion() // 1
let build = UIApplication.sharedApplication.applicationBuild() // 80
let both = UIApplication.sharedApplication.versionBuild() // 1(80)

//Returns app's version number

public static var appVersion: String? {
    return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}

//Return app's build number

public static var appBuild: String? {
    return Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as? String
}

This code works for Swift 3, Xcode 8:

let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "0"
let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "0"

For Swift 3,Replace NSBundle with Bundle and mainBundle is replaced simply by main.

let AppVersion = Bundle.main.infoDictionary!["CFBundleVersion"] as! String

[Update: Xcode 6.3.1] I tried all of the above and none of these work in Xcode 6.3.1 but I found that this does:

(NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as? String)!

Another option is to define in the AppDelegate the variables:

var applicationVersion:String {
    return NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
}
var applicationBuild:String  {
    return NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleVersionKey as String) as! String
}
var versionBuild:String  {
    let version = self.applicationVersion
    let build = self.applicationBuild
    return "version:\(version) build:(\(build))"
}

that can be referenced as variables in the AppDelegate


Swift 3 :

let textVersion
 = Bundle.main.infoDictionary?["CFBundleVersion"] as? String

SWIFT 3 Version

if let infoPath = Bundle.main.path(forResource: "Info.plist", ofType: nil),
        let infoAttr = try? FileManager.default.attributesOfItem(atPath: infoPath),
        let infoDate = infoAttr[.creationDate] as? Date
{
    return infoDate
}
return Date()

To have result for framework you can use

let version = Bundle(for: type(of: self)).infoDictionary?["CFBundleShortVersionString"] as? String

or in tests

let version = Bundle(for: <SomeClass>.self).infoDictionary?["CFBundleShortVersionString"] as? String

참고URL : https://stackoverflow.com/questions/24501288/getting-version-and-build-info-with-swift

반응형