Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 980
Rob Agar Avatar asked Feb 24 '11 00:02

Rob Agar


People also ask

What is a plist file?

In most cases, a .plist file is a XML file stored as a dictionary. We can edit property lists either in the property list editor or in the plists. You’ll notice there are key tags and string tags and one dict tag.

Why use a plist instead of a global constant?

Instead of setting global constants, I’ll make a plist and load it into the classes that need it. A small data file is a lot easier to change than finding a constant in a mass of code. Secondly, I use it as I did here to load tables and pickers with static data.

Can you write plists in a view?

// Do any additional setup after loading the view, typically from a nib. Build and run. You will get different results than your first run I’m conservative with using plists. While you can write plists, I never do.

How conservative are you with using plists?

I’m conservative with using plists. While you can write plists, I never do. I’m of the thought that property lists should be a read-only at run time. While there are several reasons for that, my biggest is there are much better ways to save data than plists. I use them for two things only.


3 Answers

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
like image 139
Alex Deem Avatar answered Oct 19 '22 05:10

Alex Deem


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.

like image 36
Chris McAloney Avatar answered Oct 19 '22 06:10

Chris McAloney


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
like image 1
rudy Avatar answered Oct 19 '22 05:10

rudy