简体   繁体   English

iOS:在构建阶段更改info.plist

[英]iOS: Changing info.plist during build phase

I am trying to do the following: 我正在尝试执行以下操作:

  1. During the build phase, open a plain text file and read the text 在构建阶段,打开纯文本文件并阅读文本
  2. 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. 如果有人可以指导我实现这一点,那就太好了。

Probably the simplest way is to use PlistBuddy. 可能最简单的方法是使用PlistBuddy。 I have a Run Script phase that looks like this: 我有一个Run Script阶段,如下所示:

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 after the 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. 编辑01/17:更新以避免不必要的复制或目标签名。 You don't want to touch Info.plist unless something really changes, otherwise Xcode will treat it (and thus the target) as modified. 你不想触摸Info.plist,除非真的发生了变化,否则Xcode会将它(以及目标)视为已修改。 Checking previous value CFBundleVersion can significantly speed up builds — it saved me several seconds on noop build. 检查以前的值CFBundleVersion可以显着加快构建速度 - 它在noop构建上节省了几秒钟。

Yes you can. 是的你可以。 I would do it in three steps: 我会分三步完成:

  1. Write a shell script that is run before the first build-phase. 编写在第一个构建阶段之前运行的shell脚本。 Let this script set an environment variable. 让这个脚本设置一个环境变量。
  2. Enable "Expand Build Settings in Info.plist" for you project. 为您的项目启用“在Info.plist中展开构建设置”
  3. Use the environment variable in the plist file like ${MY_COOL_SETTING} . 在plist文件中使用环境变量,如${MY_COOL_SETTING}

@PeyloW offers one way to do it. @PeyloW提供了一种方法。 The other way to do it is to add a Run Script build step. 另一种方法是添加Run Script构建步骤。 In that step you can rewrite your Info.plist anyway you like. 在该步骤中,您可以随意重写Info.plist。 I do this all the time to set the svnversion. 我一直这样做来设置svnversion。

I recommend putting your script in a file, and then putting . myscript.sh 我建议将脚本放在一个文件中,然后放入. myscript.sh . myscript.sh in the Run Script phase. Run Script阶段的. myscript.sh This is easier to understand and maintain than putting the entire script directly in Xcode. 这比将整个脚本直接放在Xcode中更容易理解和维护。

I have a script file that puts a build number into a field in my info.plist. 我有一个脚本文件,它将内部版本号放入我的info.plist中的一个字段中。 I put some place holder text in the info.plist in project and then the script just replaces it. 我在项目的info.plist中放了一些占位符文本,然后脚本就会替换它。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM