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?

Answers


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.


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

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)


This link worked for my case, but I would just add that it is a nice thing to add the project path and the folder where this auto generated class is put. So in my case it is:

echo "$fileContent" > ${SRCROOT}/MyProject/Models/AppInfo.swift

Here is a nice post about relative paths:

Relative paths on Xcode scripts


Need Your Help

Updating records codeigniter

php database codeigniter sql-update

I am trying to update a table where id of the row is entered and title is selected through a dropdown list, but I have two problems.

How do I read the content of an Excel spreadsheet using Ruby?

ruby rubygems spreadsheet parseexcel

I am trying to read an Excel spreadsheet file with Ruby, but it is not reading the content of the file.