iOS: Changing info.plist during build phase
I am trying to do the following:
- During the build phase, open a plain text file and read the text
- Change the value of a property in info.plist to the value obtained in step 1.
Can I write开发者_运维问答 a shell script for this?
It will be great if someone can guide me to achieve this.
Probably the simplest way is to use PlistBuddy. I have a Run Script phase that looks like this:
BUILD_NUMBER=`git rev-list HEAD --count`
INFO_PLIST="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
if [ -f "$INFO_PLIST" ] ; then
oldversion=`/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$INFO_PLIST"`
fi
if [ "$BUILD_NUMBER" != "$oldversion" ] ; then
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILD_NUMBER" "$INFO_PLIST"
fi
(Note that starting with Xcode 6, you have to run this after the Copy Bundle Resources phase, because Info.plist
isn't copied to the target location until then and PlistBuddy would fail.)
Edit 01/17: Updated to avoid unnecessary copying or signing of targets. You don’t want to touch Info.plist unless something really changes, otherwise Xcode will treat it (and thus the target) as modified. Checking previous value CFBundleVersion
can significantly speed up builds — it saved me several seconds on noop build.
Yes you can. I would do it in three steps:
- Write a shell script that is run before the first build-phase. Let this script set an environment variable.
- Enable "Expand Build Settings in Info.plist" for you project.
- Use the environment variable in the plist file like
${MY_COOL_SETTING}
.
@PeyloW offers one way to do it. The other way to do it is to add a Run Script build step. In that step you can rewrite your Info.plist anyway you like. I do this all the time to set the svnversion.
I recommend putting your script in a file, and then putting . myscript.sh
in the Run Script phase. This is easier to understand and maintain than putting the entire script directly in Xcode.
I have a script file that puts a build number into a field in my info.plist. I put some place holder text in the info.plist in project and then the script just replaces it. It only increments the build number on release builds. On development builds it just says they are a development build.
if [ "$BUILD_STYLE" = "Release" ]
then
if [ ! -f build-number ]; then
echo 0 > build-number
else
expr `cat build-number` + 1 > build-number.new
mv build-number.new build-number
fi
perl -pi -e s/BUILD_NUMBER_PLACEHOLDER/`cat build-number`/ $BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app/Contents/Info.plist
else
perl -pi -e s/BUILD_NUMBER_PLACEHOLDER/`echo Development`/ $BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app/Contents/Info.plist
fi
精彩评论