Search code examples
iphoneobjective-ciosxcodexcconfig

Pre-processor macros in xcconfig files


is is possible to use macros in config files? I want to achieve something like:

if iPad
set variable to 1
else
set variable to 0

Is that possible? I would rather not use scripts for this.


Solution

  • You generally should check this at runtime rather than compile time. See iOS - conditional compilation (xcode).

    If you don't do it that way, I typically recommend using different targets as hinted at by @Robert Vojta.

    That said, I can imagine cases where this would be useful in some piece of shared code. So...

    There is an xcconfig variable you can use called TARGETED_DEVICE_FAMILY. It returns 1 for iPhone and iPod Touch, and 2 for iPad. This can be used to create a kind of macro. I don't highly recommend this approach, but here's how you do it. Let's say you were trying to set some value called SETTINGS:

    // Family 1 is iPhone/iPod Touch. Family 2 is iPad
    SettingsForFamily1 = ...
    SettingsForFamily2 = ...
    SETTINGS = $(SettingsForFamily$(TARGETED_DEVICE_FAMILY))
    

    I've done this a few times in my projects (for other problems, not for iPad detection). Every time I've done it, a little more thought has allowed me to remove it and do it a simpler way (usually finding another way to structure my project to remove the need). But this is a technique for creating conditionals in xcconfig.