views:

128

answers:

2

Is there a build preprocessor I can use, like #if or #ifdef to check whether my current xcode project is being built for iphone or ipad?

+1  A: 
NSString *deviceType = [UIDevice currentDevice].model;

if([deviceType isEqualToString:@"iPhone"]) {
    //iPhone
}
else if([deviceType isEqualToString:@"iPod touch"]) {
    //iPod Touch
}
else {
    //iPad
}

You cannot, as far as I am concerned, use #if or #ifdef to do this but, it is supported because Obj-C is a strict superset of C.

Related: Determine device (iPhone, iPod Touch) with iPhone SDK

thyrgle
Thats what I was afraid of since they essential run the same applications.
Justin Meiners
Obj-C is a strict superset of Obj-C???
Alexander Rafferty
Thanks for the help though. I think I can think of a configuration that I can setup that creates a #define based on this.
Justin Meiners
@Alexander Rafferty: Oops sorry. Yeah, C is what I meant to say.
thyrgle
@Alexander Ha I know what he means.
Justin Meiners
You can't do a compile-time check for a "universal binary" because it's being *built for both*. If you build separate iPad and iPhone apps, then there are many ways, including defining your own compiler macros for the different targets.
tc.
+6  A: 

Some ideas in the comment section of this blog

http://greensopinion.blogspot.com/2010/04/from-iphone-to-ipad-creating-universal.html

Mostly using

UI_USER_INTERFACE_IDIOM()

Such as:

#ifdef UI_USER_INTERFACE_IDIOM()
  #define IS_IPAD() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#else
  #define IS_IPAD() (false)
#endif
Lou Franco
That is a little hacky but that is pretty cool.
Justin Meiners
this is actually suggested in apple lecture from wwdc.
Vojto