Rob Agar
Rob Agar

Reputation: 12459

Info.plist value as C++ #define

In a C++ iOS project (or any other Mac OS), is there a simple way of making a value available both to the Info.pList settings, and to the code in the form of a preprocessor macro?

Ideally, I would like to have something like this

C++ code:

#define MY_VERSION_STRING "1.0"

Info.pList

CFBundleVersion: ${MY_VERSION_STRING}

Or alternatively, is there a way of getting values from the .pList in c++? (Without manually parsing the .pList as xml.)

Upvotes: 6

Views: 2833

Answers (6)

tontonCD
tontonCD

Reputation: 358

If you use #define..., you shoud use in the .plist key, MY_VERSION_STRING, and not ${MY_VERSION_STRING}. This works too with the "Info.plist Preprocessor Prefix File". In both cases don't forget to set "Preprocess Info.plist File".

Upvotes: 0

rudy
rudy

Reputation: 1712

This is all readily doable without involving PlistBuddy at all, entirely using build settings.

you create a user defined build setting for your project/target either in the Xcode UI or if you're familiar with xcconfig files you can define it there in a completely textual = form.

  1. you create your setting MY_VERSION_STRING with a value of 1.0 as your build setting either in Xcode or in an xcconfig file.
  2. in your Info.plist your CFBundleVersion line would have a value of ${MY_VERSION_STRING}
  3. you turn on Info.plist preprocessing
  4. lastly, make use of GCC_PREPROCESSOR_DEFINITIONS build variable. for that build setting you can specify a value of MY_VERSION_STRING=${MY_VERSION_STRING} which will result in your defined and shared build setting definition available in to your c/c++/obj-c code as if you had created it as a #define

Upvotes: 1

Chris McAloney
Chris McAloney

Reputation: 21

I know this has already been answered, but I'll add my two cents for posterity. As Richard mentioned above, Xcode has a couple of options for preprocessing Info.plist files -- the most relevant to the current question are "Preprocess Info.plist" and "Info.plist Preprocessor Prefix File".

If your version information is defined in, say ver.h, you can include ver.h as the prefix file and refer to the version macro directly from Info.plist.

Upvotes: 2

Rob Agar
Rob Agar

Reputation: 12459

In case anyone wants to do the same thing, this is the script I added to the target before the compilation phase:

VERSION=`/usr/libexec/PlistBuddy -c 'Print :CFBundleVersion' Info.plist`

echo "#define VERSION_STRING L\"$VERSION\"" > Version.h

Upvotes: 0

user405725
user405725

Reputation:

Property list can also store arrays or some binary data. How do you represent that? It is very domain-specific. So if you know exactly how do you want each type to be represented in C++, you have to either parse plist file and generate C++ code, be that preprocessor directives, or some code defining arrays, enums etc. There are PlistBuddy and plutil tools available, but they probably won't be much of a help. The easiest way for me would be to use perl, see Using Perl to Manage Plist Files for details.

Good luck!

Upvotes: 0

Alex Deem
Alex Deem

Reputation: 4805

Probably not the best solution, but you could use the /usr/libexec/PlistBuddy utility in a build script to generate a .h file containing a define with a value extracted from the plist.

To output a value from a plist:

/usr/libexec/PlistBuddy -c 'Print :Path:To:Key' filename.plist

Upvotes: 2

Related Questions