Add git commit SHA to iOS application

You can do it in Schemes. Open your scheme (edit), expand Build in your scheme, click on Pre-Actions, click on + button, select New Run Script Action and write some script which gets SHA and modifies some header file where you can put SHA (easiest way is #define GIT_SHA @"...") and use GIT_SHA in your app in a place where you can display it.


For Swift

Git log format link

Other Swift Flags enter image description here

Run Script

version=$(git rev-parse --verify HEAD | cut -c 1-10)
commitDate=$(git log -n 1 HEAD --pretty=format:"%h - %cd" | cut -c 12-)

filesource="//\n//  GitVersion.swift\n//\n//  Commit Date:$commitDate\n//\n\n#if DEBUG\nlet gitVersion = \"$version\"\n#endif"
cd ${SOURCE_ROOT}/${PROJECT_NAME}

echo -e "$filesource" > GitVersion.swift

touch GitVersion.swift

GitVersion.swift

//
//  GitVersion.swift
//
//  Commit Date: Tue Jun 19 11:09:55 2018 +0900
//

#if DEBUG
let gitVersion = "2fd2f0315d"
#endif

For Swift Projects

Source: https://github.com/ankushkushwaha/AppVersionInXcode

  1. Add build script Run Script

    #/bin/sh

    version=$(git rev-parse --verify HEAD | cut -c 1-7)

    fileContent="// DO NOT EDIT, // IT IS A MACHINE GENERATED FILE

    // AppInfo.swift // import Foundation class AppInfo { let version: String let build: String let gitCommitSHA: String = \"$version\" init?() { guard let version = Bundle.main.infoDictionary?[\"CFBundleShortVersionString\"] as? String, let build = Bundle.main.infoDictionary?[\"CFBundleVersion\"] as? String else { return nil } self.version = version self.build = build } }" echo "$fileContent" > AppInfo.swift

  2. Move/Drag the run script above the 'compile sources'

  3. Now Build your project, It will create a file AppInfo.swift in project's root folder
  4. Drag an drop AppInfo.swift file into your Xcode project navigator.
  5. Use as below

    guard let info = AppInfo() else { return } let infoText = "AppVersion: (info.version) \nBuild: (info.build) \nGit hash: (info.gitCommitSHA)" print(infoText)


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.