6

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.

John Lane
  • 1,112
  • 1
  • 14
  • 32

3 Answers3

9

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.

Community
  • 1
  • 1
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • 1
    That's a great tip! Thanks, but what do you do if the TARGETED_DEVICE_FAMILY flag is universal, i.e. returns 1,2? – John Lane Oct 05 '12 at 08:09
4

AFAIK it's not possible. But if you want to solve simple task - lot of common settings and just few variables have different values, you can do this:

generic.xcconfig:

settings for both configs

ipad.xcconfig:

#include "generic.xcconfig"
ipad-specific-settings

iphone.xcconfig

#include "generic.xcconfig"
iphone-specific-settings

This can solve your condition need. I do use this schema frequently.

zrzka
  • 20,249
  • 5
  • 47
  • 73
0

That's not possible. Configuration files are not preprocessed (and compiled). What are you trying to do?

Sulthan
  • 128,090
  • 22
  • 218
  • 270