10

I'm looking for a way to dynamically add in information about the application during the build process of an iOS application.

During testing, it would be great to know when the application I have installed on my device was built and possibly who built it would be a good to know as well.

I'm envisioning a section in settings.app that would give basic build information for debugging purposes. I don't want to have to manually update a build information file before each build - the data should be generated dynamically.

sha
  • 17,824
  • 5
  • 63
  • 98
Josh Schumacher
  • 198
  • 1
  • 9
  • 1
    These similar questions might provide some ideas for how to stamp values into files at build time: [Insert Subversion revision number in Xcode](http://stackoverflow.com/questions/372218/insert-subversion-revision-number-in-xcode) and [How can I display the application version revision in my application's settings bundle?](http://stackoverflow.com/questions/877128/how-can-i-display-the-application-version-revision-in-my-applications-settings-b) – Brad Larson Jan 31 '11 at 23:42
  • Thanks Brad, those are great resources. – Josh Schumacher Feb 11 '11 at 00:49

2 Answers2

13

You can also use standard macro __DATE__ which will result string like "Jun 25 1980" of course with proper current date of build.

sha
  • 17,824
  • 5
  • 63
  • 98
  • This is the way to go here. – PakitoV Nov 22 '12 at 15:45
  • 3
    In order to transform that __DATE__ macro to a NSString check this question: http://stackoverflow.com/questions/2862469/iphone-sdk-objective-c-date-compile-date-cant-be-converted-to-an-nsdate – PakitoV Nov 23 '12 at 10:55
  • 2
    You can also use the `__TIME__` macro to add the time to the date. – rmaddy Dec 14 '16 at 16:56
7

You can write a shell script build phase in Xcode that runs at the end of your build process. In this phase you can use the defaults command to write data to an arbitrary file. I've used this technique to write to the Info.plist file, but you can write to any file you want[1].

Here's a sample script to write the current git version to Info.plist:

infoplist="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
gitversion="$(cd "$SRCROOT" && git describe --always --dirty 2>/dev/null)"
if [[ -n "$gitversion" ]]; then
    defaults write "${infoplist%.plist}" GitVersion "$gitversion"
fi

You should be able to adapt this to point to the file you want (e.g. your Settings bundle) and write the info you want.

[1] Be careful if you write to Info.plist, Xcode has bugs that can prevent it from realizing Info.plist changed during the build, which can break the provisioning when doing a device build.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • Terrific idea, however... I do this to save a key BuildDateTime. I can read [[NSBundle mainBundle] objectForInfoDictionaryKey:@"BuildDateTime"] fine in the simulator, but when I build for AdHoc distribution, and load the app on a device, [[NSBundle mainBundle] objectForInfoDictionaryKey:@"BuildDateTime"] returns null. Any ideas? – mputnamtennessee May 15 '12 at 18:01
  • @mputnamtennessee: Have you tried opening up your ad-hoc build and checking the Info.plist by hand? – Lily Ballard May 15 '12 at 18:47