Possible Duplicate:
Xcode project’s “Build number”
I’m using this script to update CFBundleVersion during build in Xcode 4.5.2 (app for iOS):
#!/bin/bash
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"
It seems to work fine and every time I build my app “build number” (CFBundleVersion) increases by 1.
The odd behavior happens when I try to retrieve CFBundleVersion programmatically using:
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]
or
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
And this is what I get:
CFBundleVersion 1 -> I get 1
CFBundleVersion 2 -> I get 1 odd
CFBundleVersion 3 -> I get 3
CFBundleVersion 4 -> I get 3 odd
CFBundleVersion 5 -> I get 5
CFBundleVersion 6 -> I get 5 odd
CFBundleVersion 7 -> I get 7
CFBundleVersion 8 -> I get 7 odd
CFBundleVersion 9 -> I get 9
CFBundleVersion 10 -> I get 10
CFBundleVersion 11 -> I get 10 odd
CFBundleVersion 12 -> I get 12
CFBundleVersion 13 -> I get 13
CFBundleVersion 14 -> I get 14
CFBundleVersion 15 -> I get 14 odd
CFBundleVersion 16 -> I get 16
CFBundleVersion 17 -> I get 16 odd
CFBundleVersion 18 -> I get 18
CFBundleVersion 19 -> I get 18 odd
CFBundleVersion 20 -> I get 20
CFBundleVersion 21 -> I get 21
CFBundleVersion 22 -> I get 22
and so on...
Outputs change each time, but the odd behavior persists in simulator and on the device.
If I remove the script and put CFBundleVersion manually in place, everything goes smoothly.
Does someone know why? And how can I fix it?
Thanks!!!
The problem is that Xcode does not check anymore for modified files after the run script has been executed. So even if the run script updates the Info.plist file, Xcode does not copy the updated file into the application bundle.
As a workaround, you can create an additional target (i.e. an “Aggregate” target) to your project and add the run script to that additional target, instead of your main target.
The
$INFOPLIST_FILEenvironment variable is not defined for the additional target, but you can use the actual (project relative) path instead, i.e. replace$INFOPLIST_FILEbyThen drag the additional target into the “Target Dependencies” of your main target, so that the additional target is always built first.
EDIT: I just found a more elegant solution here: https://stackoverflow.com/a/11112042/1187415, using a “Pre-action” script instead of an additional target.