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.

Screen Shot 2013-09-06 at 8.30.55 AM

 

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:

Screen Shot 2013-09-06 at 8.42.31 AM

 

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.”

Screen Shot 2013-09-06 at 8.45.08 AM

 

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

Screen Shot 2013-09-06 at 8.48.34 AM

 

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

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.

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

Screen Shot 2013-09-06 at 9.08.37 AM

 

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.

Related

Sign up for the O'Reilly Programming Newsletter to get weekly insight from industry insiders.
topic: Programming