ios 如何从 xcode 获取您自己的应用程序版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7957794/
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 22:08:29  来源:igfitidea点击:

How to get your own app version from xcode?

iphoneiosxcodeipad

提问by Valentin Radu

I wonder if there is a way to get your own app version in code after you put it in the "Summary" tab in xCode. One way seems to be to search Info.plistfor CFBundleVersionkey, but is there any other, easier, more convenient way?

我想知道在将您自己的应用程序版本放入 xCode 的“摘要”选项卡后,是否有办法在代码中获取它。一种方法似乎是搜索Info.plistCFBundleVersion关键,但没有任何其他的,更简单,更方便的方法?

回答by Chris Ledet

You can find it in the main bundle. I think it's something like:

您可以在主捆绑包中找到它。我认为它是这样的:

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

回答by Anthony

I typically use the Build number (CFBundleVersion) to keep track of, well, the number of builds, and the Version number (CFBundleShortVersionString) for marketing purposes. I use a run script to auto-increment my build number and I update the version number manually before each new release. So if you want to include the actual Version number in your code as opposed to the Build number, use this:

我通常使用版本号 ( CFBundleVersion) 来跟踪版本号 ( CFBundleShortVersionString) 以及用于营销目的的版本号 ( )。我使用运行脚本自动增加我的内部版本号,并在每个新版本之前手动更新版本号。因此,如果您想在代码中包含实际版本号而不是内部版本号,请使用以下命令:

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

or

或者

[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

Here's the run script that increments the build number for anyone interested:

这是为任何感兴趣的人增加内部版本号的运行脚本:

#!/bin/bash

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"

回答by Aravindhan

NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

回答by bjd23

For Swift:

对于斯威夫特:

Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString")