how to get XCode to add build date & time to Info.plist file
Asked Answered
B

3

16

Finally... after a couple years of watching and a month of participating, I have a chance to ask you guys a question of my own.

My boss doesn't trust me (or any process) to increment a build number, he also wants to have a build date & time baked into the app. I'd like to put this into the usual Info.plist file.

I found this related question:

Build information in iOS Application (date/time app was built)

and based on the answers there, I went into the Scheme Editor and added the script below to the "Post-Action" section of the Build phase:

infoplist="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
builddate=`date`
if [[ -n "$builddate" ]]; then
    defaults write "${infoplist%.plist}" BuildDate "${builddate}"
fi

In XCode, my Scheme Editor window looks like this:


(source: myke at maniac.deathstar.org)

Unfortunately, BuildDate never gets written into Info.plist.

Changing "${builddate}" to "$builddate" doesn't work either. I added this line to the script:

echo "build date is $builddate" > /tmp/result.txt

and the date appeared perfectly fine in the written out file. Writing strings into the Info.plist file from the above script works perfectly fine, annoyingly enough.

So, summed up, how to get the date to be added to the Info.plist file?

Bloomfield answered 30/10, 2011 at 9:39 Comment(1)
I didn't have any problem using your script as a post action, except that it ends up invaliding the signature. Seems to work fine as build-phase instead of a post-action, which also has the benefit of working whether you share workspaces or not. It also works better than a pre-action since a pre-action would get blitzed by changes to the real info.plist which cause a build-time overwrite.First
Z
5

The code in Michael's answer is incorrect or no longer up to date. The version below fixes an error in the set syntax and also supports build paths with spaces in them.

infoplist="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
builddate=`date`
if [[ -n "$builddate" ]]; then
    # if BuildDateString doesn't exist, add it
    /usr/libexec/PlistBuddy -c "Add :BuildDateString string $builddate" "${infoplist}"
    # and if BuildDateString already existed, update it
    /usr/libexec/PlistBuddy -c "Set :BuildDateString $builddate" "${infoplist}"
fi

Note: This change was submitted as an edit but got rejected and I don't yet have enough reputation to post a comment on his answer...

Zackaryzacks answered 25/5, 2015 at 9:53 Comment(1)
I think it's safer to add this as a "Run Script" under the build target's Build Phases, as that will ensure the value gets added to the Info.plist before the app is packaged and/or signed.Claribel
B
6

Ahhhh, I should have spent another 30 minutes (on top of the 2 hours I had already wasted) and looked at the answers for this question before posting my own:

Insert Subversion revision number in Xcode

This post-action script does the trick and works for me:

infoplist="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
builddate=`date`
if [[ -n "$builddate" ]]; then
    # if BuildDateString doesn't exist, add it
    /usr/libexec/PlistBuddy -c "Add :BuildDateString string $builddate" "${infoplist}"
    # and if BuildDateString already existed, update it
    /usr/libexec/PlistBuddy -c "Set :BuildDateString $builddate" "${infoplist}"
fi

As you can see, it's doing a bit of a hack there (adding it if it doesn't exist; setting it right afterwards).

If anyone can suggest a solution using the "defaults write" method above (which I think might be better supported than "PlistBuddy"), I'd be thrilled to find out (and of course I'll accept and use that superior answer, too).

Bloomfield answered 30/10, 2011 at 10:13 Comment(1)
defaults write must not be used to write to plist files. It's meant to only read or modify user preferences, which may be using plist files to store their values, but primarily defaults are just preferences, not files you should mess with.Claribel
Z
5

The code in Michael's answer is incorrect or no longer up to date. The version below fixes an error in the set syntax and also supports build paths with spaces in them.

infoplist="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
builddate=`date`
if [[ -n "$builddate" ]]; then
    # if BuildDateString doesn't exist, add it
    /usr/libexec/PlistBuddy -c "Add :BuildDateString string $builddate" "${infoplist}"
    # and if BuildDateString already existed, update it
    /usr/libexec/PlistBuddy -c "Set :BuildDateString $builddate" "${infoplist}"
fi

Note: This change was submitted as an edit but got rejected and I don't yet have enough reputation to post a comment on his answer...

Zackaryzacks answered 25/5, 2015 at 9:53 Comment(1)
I think it's safer to add this as a "Run Script" under the build target's Build Phases, as that will ensure the value gets added to the Info.plist before the app is packaged and/or signed.Claribel
T
3

I am using your exact code, but within the pre-action instead of the post-action, and the info.plist within the built product correctly reports the build date. In other words, you have to customize your info.plist before copying it into the built product, which sounds reasonable to me.

By the way, thanks for the suggestion, it's pretty clever and useful.

Tanner answered 30/10, 2011 at 9:54 Comment(1)
If you do this in a pre-action and then actually update the info.plist, your changes will get blitzed in the update. May not matter much, since the info.plist doesn't get updated that much.First

© 2022 - 2024 — McMap. All rights reserved.