16

终于……经过几年的观看和一个月的参与,我有机会问你们一个我自己的问题。

我的老板不相信我(或任何流程)会增加内部版本号,他还希望在应用程序中加入构建日期和时间。我想把它放到通常的 Info.plist 文件中。

我发现了这个相关的问题:

iOS 应用程序中的构建信息(构建日期/时间应用程序)

并根据那里的答案,我进入方案编辑器并将下面的脚本添加到构建阶段的“操作后”部分:

infoplist="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
builddate=`date`
if [[ -n "$builddate" ]]; then
    defaults write "${infoplist%.plist}" BuildDate "${builddate}"
fi

在 XCode 中,我的方案编辑器窗口如下所示:


(来源:myke at maniac.deathstar.org

不幸的是,BuildDate 永远不会被写入 Info.plist。

将 "${builddate}" 更改为 "$builddate" 也不起作用。我将此行添加到脚本中:

echo "build date is $builddate" > /tmp/result.txt

并且日期在写出的文件中看起来非常好。从上面的脚本中将字符串写入 Info.plist 文件可以很好地工作,但很烦人。

那么,总结一下,如何获取要添加到Info.plist文件中的日期呢?

4

3 回答 3

6

Ahhhh,我应该再花 30 分钟(在我已经浪费的 2 个小时的基础上)并在发布我自己的问题之前查看这个问题的答案:

在 Xcode 中插入 Subversion 修订号

这个动作后脚本可以解决问题并为我工作:

infoplist="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
builddate=`date`
if [[ -n "$builddate" ]]; then
    # if BuildDateString doesn't exist, add it
    /usr/libexec/PlistBuddy -c "Add :BuildDateString string $builddate" "${infoplist}"
    # and if BuildDateString already existed, update it
    /usr/libexec/PlistBuddy -c "Set :BuildDateString $builddate" "${infoplist}"
fi

如您所见,它在那里做了一些修改(如果不存在则添加它;之后立即设置它)。

如果有人可以使用上面的“默认写入”方法(我认为它可能比“PlistBuddy”得到更好的支持)提出解决方案,我会很高兴发现(当然我会接受并使用那个优越的答案,也)。

于 2011-10-30T10:13:37.767 回答
5

迈克尔答案中的代码不正确或不再是最新的。下面的版本修复了 set 语法中的一个错误,并且还支持在其中包含空格的构建路径。

infoplist="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
builddate=`date`
if [[ -n "$builddate" ]]; then
    # if BuildDateString doesn't exist, add it
    /usr/libexec/PlistBuddy -c "Add :BuildDateString string $builddate" "${infoplist}"
    # and if BuildDateString already existed, update it
    /usr/libexec/PlistBuddy -c "Set :BuildDateString $builddate" "${infoplist}"
fi

注意:此更改已作为编辑提交但被拒绝,我还没有足够的声誉对他的回答发表评论......

于 2015-05-25T09:53:51.850 回答
3

我正在使用您的确切代码,但在操作前而不是操作后,并且构建产品中的 info.plist 正确报告了构建日期。换句话说,您必须在将 info.plist复制到构建产品之前对其进行自定义,这对我来说听起来很合理。

顺便说一句,谢谢你的建议,它非常聪明和有用。

于 2011-10-30T09:54:55.227 回答