Info.plist value as C ++ #define

In a C ++ iOS project (or any other Mac OS), is there an easy way to make the value available for both Info.pList parameters and the code as 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, conversely, is there a way to get values ​​from .pList in C ++? (Without manually parsing .pList as xml.)

+6
c ++ c-preprocessor ios
source share
6 answers

This may not be the best solution, but you can use the / usr / libexec / PlistBuddy utility in the script assembly to create a .h file containing a definition with a value extracted from plist.

To infer a value from plist:

 /usr/libexec/PlistBuddy -c 'Print :Path:To:Key' filename.plist 
+2
source share

I know this has already been answered, but I will add my two cents for posterity. As Richard mentioned, Xcode has several options for preprocessing Info.plist files - the most relevant for the current question are “Preprocess Info.plist” and “Info.plist Preprocessor Prefix File”.

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

+2
source share

This is easily accomplished without involving PlistBuddy at all, making full use of the build settings.

you create a user-configurable build parameter for your project / goal either in the Xcode interface, or if you are familiar with xcconfig files, you can define it there in full text form.

  • you create your MY_VERSION_STRING parameter with a value of 1.0 as the build parameter in either Xcode or the xcconfig file.
  • in your Info.plist, your CFBundleVersion string will be $ {MY_VERSION_STRING}
  • you turn on preprocessing Info.plist
  • finally, use the build variable GCC_PREPROCESSOR_DEFINITIONS. for this assembly parameter, you can specify the value MY_VERSION_STRING = $ {MY_VERSION_STRING}, which will lead to the definition of the given and general assembly in c / C ++ / obj-c code, as if you created it as #define
+1
source share

The property list can also store arrays or some binary data. How do you imagine this? This is very domain specific. Therefore, if you know exactly how you want each type to be represented in C ++, you need to either analyze the plist file or create C ++ code, whether it be preprocessor directives or some arrays, enumerations that define the code, etc. . There are PlistBuddy and plutil tools available, but they probably won't be very useful. The easiest way for me is to use perl , see Using Perl to manage Plist files for details.

Good luck

0
source share

In case someone wants to do the same, this is a script, I added to the goal before the compilation stage:

 VERSION=`/usr/libexec/PlistBuddy -c 'Print :CFBundleVersion' Info.plist` echo "#define VERSION_STRING L\"$VERSION\"" > Version.h 
0
source share

If you use #define ..., you use it in the .plist key, MY_VERSION_STRING, not $ {MY_VERSION_STRING}. This also works with the Info.plist preprocessor prefix file. In both cases, be sure to install the "Info.plist Preprocessing File".

0
source share

All Articles