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

发布于 2024-12-07 09:19:49 字数 127 浏览 1 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

故人爱我别走 2024-12-14 09:19:49

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

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

Gist: 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;
}

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

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

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

I have the following methods in a category on 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


Here's the equivalent in 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

我的黑色迷你裙 2024-12-14 09:19:49

以下是在 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)")

Here's what worked on 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)")
め七分饶幸 2024-12-14 09:19:49

在 Objective C 中:

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

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

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

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

In Objective C:

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

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

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

NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; 
萌逼全场 2024-12-14 09:19:49

[Swift 版本: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!
}

[Swift version: 5.2]

All iOS apps must store an app version number in their Info.plist file, but there’s no build-in way to get that as a string you can use in your code.

I have created one small extension to UIApplication that reads the Info.plist file and returns a version number automatically.

Here’s the code:

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

Inside Your Controller

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


func setUpCurrentVersion(){
    tv_version.text = "v" + UIApplication.appVersion!
}
花伊自在美 2024-12-14 09:19:49

在 Swift 中,您可以通过以下方式获取捆绑版本:

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

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

versionLabel.text = "Version:" + version

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
比忠 2024-12-14 09:19:49

我的一个开源项目,应用程序更新跟踪器,以类方法的形式提供此功能(以及更多功能):

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

您将像这样使用它:

#import "AppUpdateTracker.h"

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

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

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

You would use it like so:

#import "AppUpdateTracker.h"

NSLog(@"short version: %@", [AppUpdateTracker getShortVersionString]);
NSLog(@"long version: %@", [AppUpdateTracker getLongVersionString]);
耳钉梦 2024-12-14 09:19:49

因此,这里分别提供了这两个版本的 Swift 版本:

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

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

https://github。 com/goktugyil/EZSwiftExtensions

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

岁吢 2024-12-14 09:19:49

仅供参考

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

Just for note

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文