Odd behavior when retrieving CFBundleVersion

Possible duplicate:
Xcode Projects "Build Number"

I am using this script to update CFBundleVersion at build time in Xcode 4.5.2 (iOS app):

#!/bin/bash
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

      

Everything seems to be fine and every time I create my application "build number" (CFBundleVersion) it increases by 1.

The odd behavior happens when I try to get the 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...

      

The outputs change each time, but the odd behavior persists in the simulator and on the device. If I remove the script and set the CFBundleVersion manually, everything goes smoothly.

Does anyone know why? And how can I fix this? Thank!!!

+3


source to share


1 answer


The problem is that Xcode no longer checks for changed files after running the script. Therefore, even if running the script updates the Info.plist file, Xcode does not copy the updated file into the app bundle.

As a workaround, you can create an additional target (ie Aggregate target) for your project and add a run script to that secondary target instead of your main target.

The environment variable is $INFOPLIST_FILE

not defined for additional purpose, but you can use an actual (relative project) instead, i.e. replace $INFOPLIST_FILE

with



YourMainTarget/YourMainTarget-Info.plist

      

Then drag the secondary target to the Target Dependencies of your primary target so that the secondary target is created first.

EDIT: I found a more elegant solution here: fooobar.com/questions/106217 / ... using an additional target "Pre-action" script instead.

+2


source







All Articles