我想在我的应用程序中显示我的项目构建时的当前 git SHA。在 iOS 项目中以最少的努力完成这项工作的好方法是什么?
最佳答案
Version 2.17. Build a85b242.
如果你想像上面这样添加一个漂亮的版本控制,只需按照以下步骤操作:
- 在 Xcode 中打开
Build Phases
- 按
添加构建阶段
- 按
添加运行脚本构建阶段
。您可以在顶部菜单 编辑器 中找到它。将脚本行拖到Target Dependencies
之后的位置。 - 将 Shell 行设置为
/bin/sh
将下面的脚本设置到脚本字段。不要忘记将 Sources 更改为您的文件路径,
GitVersion.h
应该在该位置。例如:version=$(git rev-parse --verify HEAD | cut -c 1-7) curdate=$(date +"%d.%m.%y") filesource="//\n// GitVersion.h\n//\n// Created by sig on $curdate.\n//\n\n#ifndef GitVersion_h\n#define GitVersion_h\n\n#define GIT_SHA_VERSION @\"$version\"\n\n#endif" cd ${SOURCE_ROOT}/${PROJECT_NAME} echo -e "$filesource" > Sources/GitVersion.h touch Sources/GitVersion.h
导入
GitVersion.h
文件到Xcode项目粘贴这些行:
NSDictionary *info = [[NSBundle mainBundle] infoDictionary]; NSString *version = [info objectForKey:@"CFBundleShortVersionString"]; NSString *app_version = [NSString stringWithFormat:@"Version %@. Build %@.", version, GIT_SHA_VERSION]; NSLog(@"app_version : %@", app_version);
Fully documented answer with images and described advantages could be found here.
关于iphone - 将 git commit SHA 添加到 iOS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12388553/