2

I am new be sh script. I did research on how to auto increase build number and version number. I know how to create an script, and let XCode to invoke that script when I build the app. But I don't see any solution can handle my task.

My question is:

Is there any existing script can do both following two things?

1. increase minor version by archive when using -AppStore scheme.
2. increase build version by build.

P.S.

I found Version vs build in XCode 4 , I have implement @nekno 's solution.

In Xcode 4.2:

Load your Xcode project. In the left hand pane, click on your project at the very top of the hierarchy. This will load the project settings editor. On the left-hand side of the center window pane, click on your app under the TARGETS heading. You will need to configure this setup for each project target. Select the Build Phases tab. At the bottom right, click the Add Build Phase button and select Add Run Script. Drag-and-drop the new Run Script phase to move it to just before the Copy Bundle Resources phase (when the app-info.plist file will be bundled with your app). In the new Run Script phase, leave the Shell: /bin/sh value alone. Copy and paste the following into the script area for integer build numbers:

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

As @AlonAmir contributed, you can use the following script instead for hex build numbers:

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

And if you have a Settings bundle where you show the Version and Build, you can add the following to the end of the script to update the version and build. Note: Change the PreferenceSpecifiers values to match your settings. PreferenceSpecifiers:2 means look at the item at index 2 under the PreferenceSpecifiers array in your plist file, so for a 0-based index, that's the 3rd preference setting in the array.

productVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$INFOPLIST_FILE")
/usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:2:DefaultValue $buildNumber" Settings.bundle/Root.plist
/usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:1:DefaultValue $productVersion" Settings.bundle/Root.plist

And if you have a universal app for iPad & iPhone, then you can also set the settings for the iPhone file:

/usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:2:DefaultValue $buildNumber" Settings.bundle/Root~iphone.plist    
/usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:1:DefaultValue $productVersion" Settings.bundle/Root~iphone.plist

But I got error:

Script-E14D3F9517E94A3300ABC5CA.sh: line 4: 1.0.18 + 1: syntax error: invalid arithmetic operator (error token is ".0.18 + 1")
Command /bin/sh failed with exit code 1

I guess it is because that script is out of date to answer this question. I'm using XCode 4.6.3

Community
  • 1
  • 1
Yi Jiang
  • 3,938
  • 6
  • 30
  • 62

0 Answers0