Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add macro BETA=1 to Xcode for certain schemes?

I want to add a new #define macro to my app, but only for certain schemes, like a beta scheme. What is the best way to do this? I know that when you are running the app in test (i.e. in simulator) it adds a DEBUG=1 macro, but I can't figure out how to add more ones.

like image 542
Jason Avatar asked Nov 24 '12 10:11

Jason


1 Answers

The best way is to use Xcode configuration files.

Add a couple of files named Beta.xcconfig and Distribution.xccconfig (or something like that) and add your macros for each kind of build.

Beta.xcconfig:

GCC_PREPROCESSOR_DEFINITIONS = $(inherited) BETA=1

Distribution.xcconfig.

GCC_PREPROCESSOR_DEFINITIONS = $(inherited) BETA=0

You can add the files easily with the new file dialog:

Create a xcconfig file

Then, you need to map each file to a build style. Got to top level project, project settings (right above targets) and click "Info" section:

Map xcconfigs to build styles

In your code you can use the macro as always:

#if BETA
// do something only in beta
#endif

If instead of assigning a value you just define the macro you should use #ifdef.

If you use several macros you may need to check that everything is working as expected looking in your build logs:

build logs with macros highlighted

like image 87
djromero Avatar answered Sep 18 '22 14:09

djromero