Upward Mobility: Buddy System

This little XCode gem can handle all your plist needs

Sometimes it’s easy to forget that along with XCode, the iOS development environment also includes a ton of useful utilities that you can leverage to make your life easier. Many of them come along with the command line tools that you can install via the Downloads pane of the XCode preferences window.

However, there are some handy tools that come bundled right in with OS X, and one of them is PlistBuddy. You can find this utility hiding in /usr/libexec, and its whole purpose in life is to make it easy to modify plist files from the command line.

To see how this tool can make your life easier, let’s consider a typical task that developers like to automate, adding a build number to the version number of an application. The version number is set in the <appname>-Info.plist file in your project, along with the bundle identifier (another value that is often modified by build scripts). Normally, we’re used to looking at this file like this:

However, if you want to use PlistBuddy, you need to know the actual key names for the values you want to modify, not the pretty name that XCode displays by default. You can do this easily by right-clicking on the file in the Project Navigator and selecting “Open as Source Code.”

Now you can see the real names of the settings. In this case, the value we want to change is CFBundleVersion.

Now we can go to the command line and start playing with PlistBuddy. Let’s start by just printing it.

1

2

3

4

James$/usr/libexec/PlistBuddy PlistExample-Info.plist

-c"Print :CFBundleVersion"

1.0

James$

The first argument to PlistBuddy is the plist file you wish to edit. Normally, the tool starts up in an interactive mode that lets you type commands to a prompt, but by using the -c option, you can enter a single command to run. In this case, we just ask for the tool to print the version number. Note that you have to prepend a colon to the key.

There are lots of things that you can do with a plist using the tool (man PlistBuddy for details), but in this case, all we want to do is update the value.

1

2

3

4

5

6

7

8

9

James$BUILD_NUM=1234

James$VERSION=`/usr/libexec/PlistBuddy PlistExample-Info.plist

-c"Print :CFBundleVersion"`

James$/usr/libexec/PlistBuddy PlistExample-Info.plist

-c"Set :CFBundleVersion $VERSION.$BUILD_NUM"

James$/usr/libexec/PlistBuddy PlistExample-Info.plist

-c"Print :CFBundleVersion"

1.0.1234

James$

If we go back to XCode, we can see that the value has been correctly updated in the file.

This is a great technique to use when automating builds with Jenkins, or as a shell script stage in your build phases. Just remember that the example I showed would add the build number on over and over again; you’d want to use some shell wizardry to extract the unchanging part of the build version on each invocation, unless you know that you’ll always be running it against a fresh copy of the project.

Get the O’Reilly Programming Newsletter

Get the Programming Newsletter

Featured Video

The growing role of software architects: “Architecture has become much more interesting now because it’s become more encompassing," says Neal Ford, software architect and meme wrangler at ThoughtWorks.