Add/Replace URL Scheme in Info.plist using bash script

↘锁芯ラ 提交于 2021-02-10 13:48:13

问题


I want to add/replace the URL scheme in Info.plist file using bash script/command.

I tried with sed command with various patterns but not succeeded.

We want to automate the build generation using Jenkins and our URL Scheme can be changed for various builds, so we want to modify the Info.plist file such that we can either add a new URL Scheme if not there or replace the existing one using script/commands.

Please suggest the command to achieve this.


回答1:


Task

  • set different url schemes for staging/production

Details

Xcode 9.2

Solution

1 create url scheme

in the run script (which is written below) /usr/libexec/PlistBuddy -c "set :CFBundleURLTypes:1:CFBundleURLSchemes:0 $SCHEME" "$INFOPLIST_MYAPP"

we have :CFBundleURLTypes:1

1 = item 1

2 add run script

INFOPLIST_MYAPP="${SRCROOT}/SM2/Application/InfoPlist/Info.plist"
SCHEME=""

case "${CONFIGURATION}" in

    "Debug_Staging" | "AdHoc_Staging" | "Test_Staging" | "Profile_Staging" )
    SCHEME="sm2dev" ;;


    "Debug_Production" | "AdHoc_Production" | "Distribution" | "Test_Production" | "Profile_Production" )
    SCHEME="sm2" ;;

    *)
        ;;
esac

/usr/libexec/PlistBuddy -c "set :CFBundleURLTypes:1:CFBundleURLSchemes:0 $SCHEME" "$INFOPLIST_MYAPP"

All Project Schemes




回答2:


Let's say your URL scheme is "myName" then you should have an entry in your info.plist file like:

<array>
    <string>myName</string>
</array>

You can change myName to newName with sed like this:

sed -i '' 's/<string>myName/<string>newName/' 'info.plist'

The <string> is important because you probably don't want to replace other myName strings with newName so it narrows it down.

Update based on OP comment:

I am not sure what your info.plist file looks like but here is a general solution. You can make a template file of what you like to add to your info.plist if the CFBundleURLTypes does not exist. For example:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>myName</string>
        </array>
        <key>CFBundleURLName</key>
        <string>com.xxx.myName</string>
    </dict>
</array>

So now you have a file called template with the previous code in it. To make this only be inserted at the location of first <dict>, change your first <dict> in the info.plist file to <myDict>. You can then check if your pattern exists and if it doesn't then insert the template and then replace myName with newName like this:

if ! grep -q "<string>myName</string>" "info.plist"; then
   sed -i '' "/<myDict>/ r template" info.plist
else
   sed -i '' 's/<myDict>/<dict>/' 'info.plist'
fi
   sed -i '' 's/<string>myName/<string>newName/' 'info.plist'

To make it cleaner you can put the previous code in a bash file insertURL.bash and then just run that file.




回答3:


May be useful for others.

I have made run script to add google reverse client at build time according to bundle id:

# @usage
# 1. Get reverse client id for google signin according to bundle id
# 2. Set in info.plist file

REVERSE_CLIENT_ID=""

if [ $PRODUCT_BUNDLE_IDENTIFIER = "com.max.bundle1" ]
then
    REVERSE_CLIENT_ID="com.googleusercontent.apps.id1"
elif [ $PRODUCT_BUNDLE_IDENTIFIER = "com.max.bundle2"]
then
    REVERSE_CLIENT_ID="com.googleusercontent.apps.id2"
fi

/usr/libexec/PlistBuddy -c "set :CFBundleURLTypes:1:CFBundleURLSchemes:0 $REVERSE_CLIENT_ID" "${PROJECT_DIR}/${INFOPLIST_FILE}"

It works fine for me. To make it work I have added in URL Types at second position like below image:

Here set :CFBundleURLTypes:1: is for second URL type as I have already one there. If you have no other url type then you have to write set :CFBundleURLTypes:0: in last line. or according to your number of url types.

Just like this you can edit some values like build number in info.plist file at runtime.




回答4:


You can use plutil tool

$INFO_PLIST_FILE=path_to_your_plist_file

$NEW_URL_STRING=new_url_string

echo "removing current url string"
plutil -remove CFBundleURLTypes.0.CFBundleURLSchemes.0 $INFO_PLIST_FILE

echo "updating with new url string"
plutil -replace CFBundleURLTypes.0.CFBundleURLSchemes.0 -string $NEW_URL_STRING $INFO_PLIST_FILE



回答5:


We can use awk command to achieve complex things easily instead of sed.

I am able to accomplish the task by the help of "Ed Morton" as in sed : Insert lines after specific pattern with newline



来源:https://stackoverflow.com/questions/23564263/add-replace-url-scheme-in-info-plist-using-bash-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!