如何在代码中获取我的iOS项目的当前版本?

我希望能够得到我的iOS项目/应用程序的当前版本作为一个NSString对象,而不必在某个文件中定义一个常量。 我不想在两个地方更改我的版本值。

当我在项目目标摘要中碰撞我的版本时,需要更新该值。

您可以获取版本和内部版本号如下:

 let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String let appBundle = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as! String 

或在Objective-C中

 NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"]; NSString * build = [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString *)kCFBundleVersionKey]; 

我在UIApplication类别中有以下方法:

 extension UIApplication { class func appVersion() -> String { return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String } class func appBuild() -> String { return Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as! String } class func versionBuild() -> String { let version = appVersion(), build = appBuild() return version == build ? "v\(version)" : "v\(version)(\(build))" } } 

要点: https //gist.github.com/ashleymills/6ec9fce6d7ec2a11af9b


这是Objective-C中的等价物:

 + (NSString *) appVersion { return [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"]; } + (NSString *) build { return [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString *)kCFBundleVersionKey]; } + (NSString *) versionBuild { NSString * version = [self appVersion]; NSString * build = [self build]; NSString * versionBuild = [NSString stringWithFormat: @"v%@", version]; if (![version isEqualToString: build]) { versionBuild = [NSString stringWithFormat: @"%@(%@)", versionBuild, build]; } return versionBuild; } 

要点: https //gist.github.com/ashleymills/c37efb46c9dbef73d5dd

这是什么在Xcode 8,Swift 3:

 let gAppVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "0" let gAppBuild = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "0" print("Version: \(gAppVersion)") print("Build: \(gAppBuild)") 

在Swift中,您可以使用以下方法获取包版本:

 let info:NSDictionary = NSBundle.mainBundle().infoDictionary! let version:String = info.objectForKey("CFBundleShortVersionString") as! String versionLabel.text = "Version:" + version 

在目标C中:

1)获取应用程序版本,你必须使用:

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

2)获取生成版本,你必须使用:

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

我的一个开源项目App Update Tracker以类方法的forms提供了这个function(以及更多):

  • + (NSString *)getShortVersionString
  • + (NSString *)getLongVersionString

你会像这样使用它:

 #import "AppUpdateTracker.h" NSLog(@"short version: %@", [AppUpdateTracker getShortVersionString]); NSLog(@"long version: %@", [AppUpdateTracker getLongVersionString]); 

因此,这两个单独的Swift版本:

 let versionNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String let buildNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") as! String 

它包括在这个回购,检查出来:

https://github.com/goktugyil/EZSwiftExtensions

只是为了说明

要获得任何键的本地化值,您应该使用CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), "CFBundleShortVersionString" as CFString)