使用Swift获取版本和构build信息

我试图访问主NSBundle检索版本和生成信息。 我想知道如何在Objective-C中用Build.text = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];检索它Build.text = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; 。 然而,我不知道从哪里开始,我试图用新的语法来写,而没有用。 谢谢你的帮助!

Swift语法有什么问题? 这似乎工作:

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

Swift 3版本

 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之后的字符在这里很重要

AppVersion方式AppNameAppVersionBuildNumber

 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))." } } 

迅速,我将它作为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))" } } 

那么你可以使用以下来获得你需要的一切:

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

此代码适用于Swift 3,Xcode 8:

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

另一种select是在AppDelegate中定义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))" } 

可以在AppDelegate中作为variables引用

对于Swift 3,用BundlereplaceNSBundle,mainBundle被main替​​换。

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

[更新:Xcode 6.3.1]我尝试了上述所有,并没有在Xcode 6.3.1工作,但我发现这样做:

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

Swift 3:

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

SWIFT 3版本

 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() 

这里是获取构build和版本的简单方法。

对于Swift 2.3

  let dictionary = NSBundle.mainBundle().infoDictionary! let version = dictionary["CFBundleShortVersionString"] as String let build = dictionary["CFBundleVersion"] as String print "Version : \(version), Build \(build)" 

对于目标C

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

让我知道如果有任何问题。 这是为我工作。