160

I would like to be able to get the current version of my iOS project/app as an NSString object without having to define a constant in a file somewhere. I don't want to change my version value in 2 places.

The value needs to be updated when I bump my version in the Project target summary.

4

8 回答 8

400

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

let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
let build = 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 {

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

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

    static var 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

于 2011-09-30T09:54:29.717 回答
14

以下是在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)")
于 2016-09-27T20:47:51.953 回答
3

在目标 C 中:

1)要获得应用程序版本,您必须使用:

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

2)要获得 Build 版本,您必须使用:

NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; 
于 2016-07-06T13:05:52.777 回答
2

[斯威夫特版本:5.2]

所有 iOS 应用程序都必须在其 Info.plist 文件中存储应用程序版本号,但没有内置方法可以将其作为可在代码中使用的字符串。

我为 UIApplication 创建了一个小的扩展,它读取 Info.plist 文件并自动返回一个版本号。

这是代码:

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

在你的控制器里面

@IBOutlet weak var tv_version: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
   
    //Display Version
    setUpCurrentVersion()
}


func setUpCurrentVersion(){
    tv_version.text = "v" + UIApplication.appVersion!
}
于 2020-08-04T16:03:23.867 回答
1

In Swift, you can get bundle version by using:

let info:NSDictionary = NSBundle.mainBundle().infoDictionary!

let version:String = info.objectForKey("CFBundleShortVersionString") as! String

versionLabel.text = "Version:" + version
于 2014-12-31T07:54:31.330 回答
0

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

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

你会像这样使用它:

#import "AppUpdateTracker.h"

NSLog(@"short version: %@", [AppUpdateTracker getShortVersionString]);
NSLog(@"long version: %@", [AppUpdateTracker getLongVersionString]);
于 2015-07-11T21:14:48.953 回答
-1

所以这里有一个分别用于这两个的 Swift 版本:

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

它包含在此回购中,请查看:

https://github.com/goktugyil/EZSwiftExtensions

于 2015-11-25T17:53:36.630 回答
-1

仅供参考

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

于 2017-04-01T05:14:25.663 回答