如何在不覆盖现有设置的情况下使用PlistBuddy添加设置?

时间:2015-12-22 15:43:52

标签: ios plist settings.bundle

我正在尝试编写一个脚本,在构建期间将设置添加到“设置”应用,而不会覆盖现有应用(如果存在)。这是我的剧本片段:

PLISTBUDDY="/usr/libexec/PlistBuddy"
SETTINGSBUNDLEPATH="$CODESIGNING_FOLDER_PATH/Settings.bundle/Root.plist"

{
$PLISTBUDDY -c "Add :PreferenceSpecifiers:0:Type string 'PSGroupSpecifier'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Add :PreferenceSpecifiers:0:Title string 'Version Information'" "$SETTINGSBUNDLEPATH"
} || {
$PLISTBUDDY -c "Set :PreferenceSpecifiers:0:Type 'PSGroupSpecifier'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Set :PreferenceSpecifiers:0:Title 'Version Information'" "$SETTINGSBUNDLEPATH"
}

{
$PLISTBUDDY -c "Add :PreferenceSpecifiers:1:Type string 'PSTitleValueSpecifier'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Add :PreferenceSpecifiers:1:Title string 'Version:'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Add :PreferenceSpecifiers:1:Key string 'appVersion'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Add :PreferenceSpecifiers:1:DefaultValue string '$APPVERSION'" "$SETTINGSBUNDLEPATH"
} || {
$PLISTBUDDY -c "Set :PreferenceSpecifiers:1:Type 'PSTitleValueSpecifier'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Set :PreferenceSpecifiers:1:Title 'Version:'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Set :PreferenceSpecifiers:1:Key 'appVersion'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Set :PreferenceSpecifiers:1:DefaultValue '$APPVERSION'" "$SETTINGSBUNDLEPATH"
}

我不想覆盖任何现有的设置。 如何将这些设置添加到PreferenceSpecifiers的末尾?

我已阅读documentation,我尝试使用Merge没有成功。

$PLISTBUDDY -c "Merge ${PROJECT_DIR}/Settings1.bundle/Root.plist" "$SETTINGSBUNDLEPATH"

编辑:在代码中添加了“try-catch”,这解决了运行脚本两次时的问题。

1 个答案:

答案 0 :(得分:1)

这首先计算所有字典PreferenceSpecifiers,如these answers中所示。然后它将遍历字典并检查该组是否已存在,因此当您第二次构建应用程序时,脚本将不会再次添加行。如果找不到该组,它将在最后一个索引之后添加行。我不确定的一件事是为什么count不是数字以及如何正确修复它(当前代码有效)。

PLISTBUDDY="/usr/libexec/PlistBuddy"
SETTINGSBUNDLEPATH="$CODESIGNING_FOLDER_PATH/Settings.bundle/Root.plist"

count=`${PLISTBUDDY} -c "Print PreferenceSpecifiers:" ${SETTINGSBUNDLEPATH} | grep "Dict"|wc -l`
let count++
let count--

FOUND=false
for index in `seq 0 $count`
do
    val=`${PLISTBUDDY} -c "Print PreferenceSpecifiers:${index}:Key" ${SETTINGSBUNDLEPATH}`
    if [ "$val" == "versionInfo" ]; then
        FOUND=true
        break
    fi
done

if [ "$FOUND" == false ]; then
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Type string 'PSGroupSpecifier'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Title string 'Version Information'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Key string 'versionInfo'" "$SETTINGSBUNDLEPATH"

    let count++
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Type string 'PSTitleValueSpecifier'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Title string 'Version:'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Key string 'appVersion'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:DefaultValue string '$APPVERSION'" "$SETTINGSBUNDLEPATH"
fi