简体   繁体   English

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

[英]How to get your own app version from xcode?

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. 我想知道在将其放入xCode的“摘要”选项卡后,是否有办法在代码中获取自己的应用程序版本。 One way seems to be to search Info.plist for CFBundleVersion key, but is there any other, easier, more convenient way? 一种方法似乎是在Info.plist中搜索CFBundleVersion键,但是还有其他更容易,更方便的方法吗?

You can find it in the main bundle. 您可以在主捆绑中找到它。 I think it's something like: 我认为它是这样的:

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

I typically use the Build number ( CFBundleVersion ) to keep track of, well, the number of builds, and the Version number ( CFBundleShortVersionString ) for marketing purposes. 我通常使用Build编号( CFBundleVersion )来跟踪构建的数量,以及用于营销目的的版本号( CFBundleShortVersionString )。 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: 因此,如果要在代码中包含实际版本号而不是内部版本号,请使用以下命令:

[[[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}"
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

对于Swift:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM