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

标签 iphone objective-c ios xcode git

我想在我的应用程序中显示我的项目构建时的当前 git SHA。在 iOS 项目中以最少的努力完成这项工作的好方法是什么?

最佳答案

Version 2.17. Build a85b242.

如果你想像上面这样添加一个漂亮的版本控制,只需按照以下步骤操作:

  1. Xcode 中打开 Build Phases
  2. 添加构建阶段
  3. 添加运行脚本构建阶段。您可以在顶部菜单 编辑器 中找到它。将脚本行拖到 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
    
  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);
    

Fully documented answer with images and described advantages could be found here.

关于iphone - 将 git commit SHA 添加到 iOS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12388553/

相关文章:

objective-c - 手势和响应链

ios - 如何将搜索栏制作成 iPhone 联系人应用程序或处理多个不完整关键字的搜索栏

iphone - 为iphone设计网站

ios - 在核心数据中遍历关系时重新获取对象

iphone - 连接耳机时如何强制从 iPhone 扬声器发出音频?

ios - Google Maps API Xcode 8 的布局问题

ios - 自 iOS 9 以来,VectorKit/OpenGL 在 MKMapView 中崩溃

objective-c - 如何使用标签为动态创建的按钮动态设置背景图像?

Iphone:如何验证消息验证码(Mac)

iphone - 以编程方式删除 UIWebView 中的选择