I know that the application version in a Mac application is defined by the Info.plist file situated inside the bundle.
The application version number in my case is not only informati开发者_如何学Gove, it defines some behaviors in my app. If someone edits this file, my application could break. The fastest solution is to compile the app version inside the executable.
Is there a common pattern for compiling the app version inside the executable?
The application version should be static/global IMHO.
In your target's build settings, go to the Versioning category and change Versioning System
from None
to Apple Generic
. Then, whenever you change the version of your application, change the Current Project Version
setting. If you don't change the other build settings, then each build will generate a file called $(PRODUCT_NAME)_vers.c
which contains two variables, defined as follows:
const unsigned char $(PRODUCT_NAME)VersionString[] = "@(#)PROGRAM:$(PRODUCT_NAME) PROJECT:$(PROJECT_NAME)-$(CURRENT_PROJECT_VERSION)\n";
const double $(PRODUCT_NAME)VersionNumber = (double)$(CURRENT_PROJECT_VERSION);
For the version number, anything after the second decimal place is cut off so that it can be represented as a double. i.e., 1.2.3 is stored as 1.2. Both variables are also given the used
attribute so that they won't be removed if you don't use them.
In order to use these variables, you need to declare them as extern in the files where they will be used. You can either do this in every file which will use them, or create a header with these declarations and include that in your files. The declarations should look like this:
extern const unsigned char $(PRODUCT_NAME)VersionString[];
extern const double $(PRODUCT_NAME)VersionNumber;
Then you can use them like normal variables.
精彩评论