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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7608632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:41:23  来源:igfitidea点击:

How do I get the current version of my iOS project in code?

iosobjective-ciphoneswift

提问by Jovan

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

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

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

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

回答by Ashley Mills

You can get the version and build numbers as follows:

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

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

or in Objective-C

或在 Objective-C 中

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

I have the following methods in a category on UIApplication:

我在一个类别中有以下方法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))"
    }
}

Gist:https://gist.github.com/ashleymills/6ec9fce6d7ec2a11af9b

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



Here's the equivalent in Objective-C:

这是 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;
}

Gist:https://gist.github.com/ashleymills/c37efb46c9dbef73d5dd

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

回答by Crashalot

Here's what worked on Xcode 8, Swift 3:

以下是适用于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)")

回答by Vaibhav Shiledar

In Objective C:

在目标 C 中:

1)For getting App version you have to use a:

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

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

2)For getting Build version you have to use a:

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

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

回答by Ravindra Kishan

In Swift, you can get bundle version by using:

在 Swift 中,您可以使用以下命令获取捆绑版本:

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

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

versionLabel.text = "Version:" + version

回答by Stunner

An open source project of mine, App Update Tracker, offers this functionality (and more) in the form of class methods:

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

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

You would use it like so:

你会像这样使用它:

#import "AppUpdateTracker.h"

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

回答by akaDuality

Just for note

仅供参考

To obtain localized value of any key you should use CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), "CFBundleShortVersionString" as CFString)

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

回答by Esqarrouth

So heres a Swift version for both of these separately:

所以这里分别为这两个版本提供了一个 Swift 版本:

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

Its included in this repo, check it out:

它包含在此 repo 中,请查看:

https://github.com/goktugyil/EZSwiftExtensions

https://github.com/goktugyil/EZSwiftExtensions