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?
Version 2.17. Build a85b242.
If you want to add a pretty versioning like this above, just follow these steps:
Build Phases
in XcodeAdd Build Phase
Add Run Script Build Phase
. You can find this in the top menu Editor. Drag script-line to the position after Target Dependencies
./bin/sh
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
Import GitVersion.h
file into Xcode project
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.