3

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!!!

Community
  • 1
  • 1
Giuseppe Garassino
  • 2,272
  • 1
  • 27
  • 47
  • I had similar issue and it helped to put "run script" in "Build Phases" as second step. When I had it last as it is by default after adding this step then I had difference 2 or 3. – Lukasz 'Severiaan' Grela Jun 10 '14 at 07:25

1 Answers1

2

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_FILE environment variable is not defined for the additional target, but you can use the actual (project relative) path instead, i.e. replace $INFOPLIST_FILE by

YourMainTarget/YourMainTarget-Info.plist

Then 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.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • The solution you pointed out in EDIT does the trick, thanks, but I don't understand why my way is wrong... I mean, if Info.plist is updated after Xcode copies it in the application bundle, I should see a difference of 1 unit every time I build my app (e.g. original CFBundleVersion is 10, I build my app, Info.plist becomes 11, I retrieve 10 and so on), but the behavior I notice seems to be "just odd" or "just even" or something like "one yes, one no, and one maybe". – Giuseppe Garassino Jan 21 '13 at 23:29