views:

2935

answers:

6

Is there a specific Xcode compiler flag that gets set when compiling for iPad?

I want to conditionally compile iPad vs iPhone/iPod Touch code for example:

#ifdef TARGET_IPAD
   code for iPad
#else
   code for iPhone
#endif

I know there is already TARGET_OS_IPHONE and TARGET_CPU_ARM in TargetConditionals.h but anything that easily and specifically targets iPad?

-Rei

+2  A: 

Instead of using compile-time flags, you should use run-time check e.g. use NSClassFromString to see if a class exists because the same app can be installed on both devices.

And because of the possibility of running the app in both devices, there isn't a built-in compile-time check whether it targets iPad or not.

KennyTM
Kenny is correct. Apple recommends runtime checking for API's and weak linking against new frameworks etc.You can use Erica Sadun's UIDevice Extensions (http://github.com/erica/uidevice-extension), to easily check what device you're running on at runtime.And as Kenny says, NSClassFromString, respondsToSelector and checking function names against NULL are your friends.
Jasarien
A: 

Currently I didn’t find anything that would let you check if you are on an iPad, but I’m also not sure if Apple recommends runtime checks. Here’s an excerpt from Apple:

In addition to your view controllers, any classes that are shared between iPhone and iPad devices need to include conditional compilation macros to isolate device-specific code. Although you could also use runtime checks to determine if specific classes or methods were available, doing so would only increase the size of your executable by adding code paths that would not be followed on one device or the other. Letting the compiler remove this code helps keep your code cleaner.

However, there is no place I could find more specific information about conditional compilation macros.

Rafael
Yes, I read this too, so I thought there would be some #define someplace but I could not find it. For now, I am checking if on iPad by using the UIScreen dimensions but that does not seem very elegant. I will take a look at the UIDevice-extension mentioned above. Thanks all! -Rei
Rei
A: 

For multiple targets sharing the same project/code, I'm doing this by editing the C flags for my iPad target.

With the [myapp]-iPad target chosen as the active target, pick Project -> Edit Active Target [myapp]-iPad. Search for "c flags" and double-click. Add a flag for "-D TARGET_IPAD". Now the symbol TARGET_IPAD will be defined only for your iPad target.

Of course, this only works if you're using separate targets for iPad and iPhone. If you're running the same binary on both, obviously there's nothing the compiler can do for you. (However, the 3.2 SDK as of the end of January doesn't even support Universal apps yet.)

(Edited; I was confused about the terminology of "Universal" apps etc.)

qwzybug
So are you saying that the mystical Universal App that Apple speaks of can't even exist yet? Interestingly, if we need to have two distinct builds, that's two different versions to submit to the app store. Apple says that's not what they want. I'm waiting to see how this plays out.
JustinXXVII
The release notes for the 3.REDACTED SDK say that building UnivREDACTEDal Apps is not supported yet. I bet it's coming soon.
qwzybug
+5  A: 

The correct API to use for run-time checking of iPad vs. iPhone/iPad Touch is:

BOOL deviceIsPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);

The UIDevice header filer also includes a convenient macro, UI_USER_INTERFACE_IDIOM(), which will be helpful if your deployment target is < iPhone 3.2.

#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)

So you could just say, negatively:

BOOL deviceIsPad = (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone);
A: 

I think this will do

-(BOOL)isDeviceAniPad
{   
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
  return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
#endif
  return NO;
}
iAP
A: 

Or -> just to be sure

-(BOOL)isDeviceAniPad
{   
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
 BOOL deviceIsPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
 return deviceIsPad;
#endif
 return NO;
}
just thinking