如何以代码获取iOS项目的当前版本?


151

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

当我在“项目”目标摘要中更改版本时,需要更新该值。

Answers:


381

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

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


1
您可以使用此[[NSBundle mainBundle] objectForInfoDictionaryKey:@“ CFBundleVersion”]代替[[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey]。前者似乎与[[NSBundle mainBundle] objectForInfoDictionaryKey一样押韵]; 所以现在变得容易保存在记忆中了
Durai Amuthan.H 13/11/22

4
@Duraiamuthan我建议使用(NSString *)kCFBundleVersionKey而不是@"CFBundleVersion"。即使输入的时间更长(自动完成功能会有所帮助),因为它是一个常量,因此会在编译时进行检查。
Ashley Mills

顺便说一句,启用此扩展会更准确,NSBundle而不是UIApplication将扩展替换为实例调用。
Zapko '16

@Zapko-敬请谅解。从概念上讲,它适合于UIApplication,因为返回的信息与应用程序有关。另外,这仅适用于main包-在任何包上将其设为实例方法是没有意义的。
Ashley Mills

您说的是build什么意思appBundle(在代码的第二行)?
user1046037

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

3

在目标C中:

1)要获取App版本,您必须使用:

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

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

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

1

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

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

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

versionLabel.text = "Version:" + version

0

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

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

您可以这样使用它:

#import "AppUpdateTracker.h"

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

0

只是为了注意

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


-1

因此,以下是这两种的Swift版本:

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

它包含在此仓库中,请查看:

https://github.com/goktugyil/EZSwiftExtensions


CFBundleVersionKey不再适用于Xcode 8和Swift3。您知道新的密钥吗?
Crashalot

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.