Question

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.

Was it helpful?

Solution

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

OTHER TIPS

schemes are only executing build configurations

macros can only be set for build configurations

make a new build configuration AND a new scheme to use it

it is a bit inconvenient :/

The alternative (that I used) would be in the build settings for your project or target.

1) Go to Project -> Target -> Build Settings
2) Search for "preprocessor macros"

Now you should be able to see all schemes defined for that project and add whatever preprocessor macros you like. Just remember to leave the $(inherited). Also you probably want to keep all the other defined macros, as in my case I had the COCOAPODS=1 definition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top