Set Info.plist Value from Build Phase

3.1k views Asked by At

I have an iOS for which I want to run a build phase that reads a value from a JSON file, export that as an environment variable and then read that in my Info.plist.

I currently have:

enter image description here enter image description here

# Build Scripts/SetAPIVersion
set -e

if ! which jq > /dev/null; then
echo "error: jq is missing. Be sure to git pull `dev-box` and run apply.sh"
exit 1
fi

export API_VERSION =$(cat ../src/version.json | jq .api)

echo "Set API Version to $(API_VERSION)!"

My application will build however the value does not appear to be set. What am I doing wrong here?

2

There are 2 answers

0
Ken Thomases On

The shell interpreter is run as a subprocess. When it exports an environment variable, that only affects that shell interpreter process and its subprocesses, but doesn't affect the parent process (i.e. Xcode) nor its sibling processes (other build phases).

You can make the shell script build phase take an input file, say Info.plist.in, and produce Info.plist from that. It would transform the input to the output however you like. For example, it could use sed to replace a special string with the value it should have. Be sure to configure the inputs and outputs of the run-script build phase as appropriate.

Alternatively, you could have the run-script build phase produce a header file that defines a macro, say api_version.h which #defines API_VERSION, #include that header file in your Info.plist, and enable preprocessing of Info.plist in the build settings. Again, make sure the inputs and outputs of the run-script phase are correct.

3
Mojtaba Hosseini On

You can use this:

plutil -replace APIVersion -string <VERSION> <PATH TO INFO>.plist

Also you can use PlistBuddy:

/usr/libexec/PlistBuddy -c "Set :APIVersion string <VERSION>" <PATH TO INFO>.plist

If versions are static numbers depending on environment, you can use project build settings user defined variable to:

user defined variable`