I have always wondered how to get a better build number in my projects, instead of having to change is manually. So, I finally googled it and found a solution. Or better yet, found several… but none working for xCode 4.3.
In xCode 4.3 you can actually change the build number manually in the project settings. I have not found a solution yet to activate an automatic build number update, however I have not search so much for that either.
So how did I tackle it? Well, when building your target in xCode you can add custom stuff xCode should do. For instance, it could run a script.
So, what I did was I opened my project and added the following line of code in an new build phase.
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" MYPROJECTDIR/MYPROJECTDIR-Info.plist)
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" MYPROJECTDIR/MYPROJECTDIR-Info.plist
Now I see you thinking “Whuuut”?? No worries, I made some images to explain it. Just so you know, you should replace MYPROJECTDIR with your project dir name and file name.
The how to
First of, open your project and click on your project in the “Project Navigator” (Pic.1).
Now, in my project you see I have build 19, right? Well, lets switch to the “Build Phases” tab (Pic.2)
At the bottom right you see a little cute button popped up, called “Add Build Phase”. Click it and choose “Add run script” (Pic.4).
Add the code I wrote above in the “Shell” text area. Now move the area upwards so you got it right below “Target Dependencies”. (Pic.4)
Now you are basically done. You could build and run to the simulator and the build number will be updated. One thing you should know though is that xCode caches the plist files for your project, so even though you build and run it might be that xCode hasn’t understood the update yet. Just open the ***-Info.plist file in finder and you’ll see the update.
Hope this helped, if so… FB LIKE IT! 😉