Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically Add Date/Time and Build Number to Info.plist Xcode

Tags:

bash

xcode

I am looking to automatically add the build date and build number to my Info.plist file for only the builds where I archive the application for iOS.

My script is as follows

#!/bin/bash
# This was taken from variations from:
# http://davedelong.com/blog/2009/04/15/incrementing-build-numbers-xcode

if [ "$CONFIGURATION" != "Release" ]
then
exit
fi


buildPlist="$SRCROOT/$PROJECT_NAME/$PROJECT_NAME-Info.plist"


# Get the existing buildVersion and buildNumber values from the buildPlist
buildVersion=$(/usr/libexec/PlistBuddy -c "Print CFBuildVersion" "$buildPlist")

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBuildNumber" "$buildPlist")

buildDate=$(date "+%s")


# Increment the buildNumber
buildNumber=$(($buildNumber + 1))

# Set the version numbers in the buildPlist
/usr/libexec/PlistBuddy -c "Set :CFBuildNumber $buildNumber" "$buildPlist"
/usr/libexec/PlistBuddy -c  "Set :CFBuildDate $buildDate" "$buildPlist"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildVersion.$buildNumber" "$buildPlist"
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $buildVersion.$buildNumber" "$buildPlist"
/usr/libexec/PlistBuddy -c "Set :CFBundleLongVersionString $buildVersion.$buildNumber.$buildDate" "$buildPlist"

Everything here is working great, and I have this script running as a build phase right after target dependencies in the build phase tab.

The only issue I am having is that the project seems to be built using the old values for Info.plist, not the new ones.

In other words, if I archive the application and examine the Info.plist file in the resulting ipa, they reflect the old values. Looking at the Info.plist in the project, however, shows that the script did indeed execute correctly, but not in time for the project to be build using the old Info.plist.

Is there any solution to this?

This seems like something that could be fixed relatively easily but I'm surprised not as many people have been asking about this issue in the articles posted on this topic.

like image 776
ecbtln Avatar asked Dec 25 '12 19:12

ecbtln


2 Answers

Turns out the answer was incredibly straightforward. All that was necessary was to turn on Info.plist preprocessing and then use the __TIMESTAMP__ preprocessor directive.

like image 166
ecbtln Avatar answered Sep 16 '22 12:09

ecbtln


The problem is that the Info.plist file is processed before anything else run. From examining the build log this involves converting it to binary and placing it inside the build directory.

To fix this, the versioning script must modify the file in the build directory, and not the file in the project directory. The path to the Info.plist in the build directory can be obtained by combining the following environmental variables which are set during the build process:

plistpath=${BUILT_PRODUCTS_DIR}/${INFOPLIST_PATH}

If you now modify the file at $plistpath you will be modifying the plist that will end up being packaged into the final application.

I use this technique to get build information into releases, with the versioning script running as the first build phrase.

like image 33
freespace Avatar answered Sep 16 '22 12:09

freespace