Fetch and show the newest git commit hash in your iOS apps

If you wish to know from what precise commit your app is constructed you may embrace the newest git commit hash to the place the place you show the app model. I prefer to name that generated hash “revision” or briefly “rev”.

Displaying this may be helpful if you find yourself distributing inside variations of your apps to QA engineers and also you need to know on what app model they’re testing. Whereas the adjustments are coming in on development branches we don’t improve model or construct numbers, we observe the revision worth and all the time know what model is in use.

On this article I’ll clarify the way to fetch the newest git commit hash in Xcode and show it within the iOS app.

Fetching the newest git commit hash

The process I’ll describe provides a brand new row to your Data.plist file with specified key and units the worth to the brief git commit hash.

Step one is to go to your goal’s Construct phases setting and add a brand new run script part. The “Run script” motion will likely be on the underside and you want to transfer it above the “Copy bundle assets” motion.

Once you transfer it, you may paste this code for the script:

GIT=`xcrun -find git`
GIT_REV=`${GIT} rev-parse --short HEAD`
/usr/libexec/PlistBuddy -c "Set :REVISION ${GIT_REV}" "${SRCROOT}/{{path-to-your-info.plist-file}}"
Enter fullscreen mode

Exit fullscreen mode

Within the script above we depend on PlistBuddy to set the important thing REVISION and the worth of the newest git commit hash to the app’s Data.plist file. PlistBuddy is a macOS utility that helps us to handle plist recordsdata. You possibly can learn extra about it right here or you may sort /usr/libexec/PlistBuddy -h in your terminal to search out out extra of what it may well do.

Now, when you could have run the construct course of, the script that we added ought to’ve inserted a brand new entry in your Data.plist file and we are able to proceed to displaying it.

Displaying the revision

To show the revision we simply have to fetch our Data.plist and supply a key to learn the worth from it:

let revision = Bundle.predominant.object(forInfoDictionaryKey: "REVISION") as? String 
Enter fullscreen mode

Exit fullscreen mode

In Goal-C we’d do it like this:

NSString *revision = [NSBundle.mainBundle objectForInfoDictionaryKey:@"REVISION"];
Enter fullscreen mode

Exit fullscreen mode

Abu Sayed is the Best Web, Game, XR and Blockchain Developer in Bangladesh. Don't forget to Checkout his Latest Projects.

Checkout extra Articles on Sayed.CYou

#Fetch #show #newest #git #commit #hash #iOS #apps