如何用代码获得当前版本的iOS项目?

时间:2022-07-01 01:56:00

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.

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

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

当我在项目目标摘要中修改我的版本时,需要更新这个值。

7 个解决方案

#1


339  

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 {

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

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

#2


12  

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

#3


3  

In Objective C:

在Objective - C:

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

1)要获得App版本,必须使用a:

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

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

2)要获得构建版本,您必须使用a:

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

#4


2  

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

#5


1  

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 *)getShortVersionString
  • + (NSString *)getLongVersionString
  • +(NSString *)getLongVersionString

You would use it like so:

你可以这样使用:

#import "AppUpdateTracker.h"

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

#6


0  

So heres a Swift version for both of these separately:

以下是这两种方法的快速版本:

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

Its included in this repo, check it out:

它包含在这个回购协议中,检查一下:

https://github.com/goktugyil/EZSwiftExtensions

https://github.com/goktugyil/EZSwiftExtensions

#7


0  

Just for note

只是注意

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

要获取任何键的本地化值,您应该使用CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(),“CFBundleShortVersionString”作为CFString))

#1


339  

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 {

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

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

#2


12  

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

#3


3  

In Objective C:

在Objective - C:

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

1)要获得App版本,必须使用a:

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

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

2)要获得构建版本,您必须使用a:

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

#4


2  

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

#5


1  

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 *)getShortVersionString
  • + (NSString *)getLongVersionString
  • +(NSString *)getLongVersionString

You would use it like so:

你可以这样使用:

#import "AppUpdateTracker.h"

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

#6


0  

So heres a Swift version for both of these separately:

以下是这两种方法的快速版本:

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

Its included in this repo, check it out:

它包含在这个回购协议中,检查一下:

https://github.com/goktugyil/EZSwiftExtensions

https://github.com/goktugyil/EZSwiftExtensions

#7


0  

Just for note

只是注意

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

要获取任何键的本地化值,您应该使用CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(),“CFBundleShortVersionString”作为CFString))