xcode iOS:在构建阶段更改 info.plist
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6912743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: Changing info.plist during build phase
提问by Mithin
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.
- 在构建阶段,打开一个纯文本文件并阅读文本
- 将 info.plist 中某个属性的值更改为步骤 1 中获得的值。
Can I write a shell script for this?
我可以为此编写一个shell脚本吗?
It will be great if someone can guide me to achieve this.
如果有人能指导我实现这一目标,那就太好了。
采纳答案by PeyloW
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}
.
- 编写一个在第一个构建阶段之前运行的 shell 脚本。让这个脚本设置一个环境变量。
- 为您的项目启用“在 Info.plist 中扩展构建设置”。
- 在 plist 文件中使用环境变量,如
${MY_COOL_SETTING}
.
回答by Václav Slavík
Probably the simplest way is to use PlistBuddy. I have a Run Script phase that looks like this:
可能最简单的方法是使用 PlistBuddy。我有一个运行脚本阶段,如下所示:
BUILD_NUMBER=`git rev-list HEAD --count`
INFO_PLIST="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
if [ -f "$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH" ] ; 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 afterthe Copy Bundle Resources phase, because Info.plist
isn't copied to the target location until then and PlistBuddy would fail.)
(请注意,从 Xcode 6 开始,您必须在 Copy Bundle Resources 阶段之后运行它,因为Info.plist
在此之前不会复制到目标位置并且 PlistBuddy 会失败。)
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.
编辑 01/17:更新以避免不必要的复制或签署目标。除非确实发生了变化,否则您不想触摸 Info.plist,否则 Xcode 会将其(以及目标)视为已修改。检查以前的值CFBundleVersion
可以显着加快构建速度——它为我在 noop 构建中节省了几秒钟。
回答by Rob Napier
@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.
@PeyloW 提供了一种方法。另一种方法是添加一个运行脚本构建步骤。在这一步中,您可以随意重写您的 Info.plist。我一直这样做来设置 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.
我建议将您的脚本放在一个文件中,然后放入. myscript.sh
运行脚本阶段。这比将整个脚本直接放在 Xcode 中更容易理解和维护。
回答by Ron Davis
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.
我有一个脚本文件,它将内部版本号放入我的 info.plist 中的一个字段中。我在项目的 info.plist 中放置了一些占位符文本,然后脚本只是替换了它。它只会增加发布版本的版本号。在开发构建上,它只是说它们是开发构建。
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