Search code examples
iphoneobjective-ciosxcodegit

Add git commit SHA to iOS application


I want to show the current git SHA of when my project was built in my App. What is a good way of doing this in an iOS project with minimal effort?


Solution

  • Version 2.17. Build a85b242.

    If you want to add a pretty versioning like this above, just follow these steps:

    1. Open Build Phases in Xcode
    2. Press Add Build Phase
    3. Press Add Run Script Build Phase. You can find this in the top menu Editor. Drag script-line to the position after Target Dependencies.
    4. Set Shell line to /bin/sh
    5. Set the script below to the Script field. Don't forget to change Sources to your path-to-file, where GitVersion.h should be. For example:

      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. Import GitVersion.h file into Xcode project

    7. Paste these lines:

      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.