将git commit SHA添加到iOS应用程序

Bes*_*esi 10 git iphone xcode objective-c ios

我想在我的应用程序中显示我的项目构建时的当前git SHA.在iOS项目中以最小的努力这样做的好方法是什么?

dig*_*ple 23

版本2.17.建立a85b242.

如果您想添加如上所述的漂亮版本,请按以下步骤操作:

  1. Build PhasesXcode中打开
  2. Add Build Phase
  3. Add Run Script Build Phase.您可以在顶部菜单编辑器中找到它.将脚本行拖到后面的位置Target Dependencies.
  4. Shell行设置为/bin/sh
  5. 将下面的脚本设置为" 脚本"字段.不要忘记将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
    
    Run Code Online (Sandbox Code Playgroud)
  6. GitVersion.h文件导入Xcode项目

  7. 粘贴这些行:

    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);
    
    Run Code Online (Sandbox Code Playgroud)

可以在此处找到完整记录的图像答案和所描述的优点.


zrz*_*zka 7

你可以在Schemes中完成.打开您的方案(编辑),展开您的方案中的Build,单击Pre-Actions,单击+按钮,选择New Run Script Action并编写一些获取SHA的脚本并修改一些头文件,您可以在其中放置SHA(最简单的方法是#define GIT_SHA @"...")并GIT_SHA在您的应用程序中使用您可以显示它的地方.

  • "写一些脚本"部分是非平凡的部分.你的答案基本上是_"我如何让Xcode在编译时做点什么"_当问题基本上是_"我如何在编译时使Xcode获得当前的Git SHA并使其可以访问Objective-C/Swift代码" _. (4认同)