Is there a build preprocessor macro I can check, with #if
or #ifdef
to determine if my current Xcode project is being built for iPhone or iPad?
EDIT
As several answers have pointed out, often apps are universal, and the same binary can run on both devices. Conditional behavior between these very similar devices should be solved at runtime rather than compile time.
I use the following code for AppleTV 4 because UIUserInterfaceIdiomUnspecified, doesn't seem to work, nor can I find any other enums:
I used to use this for iPad and such before the dark times, before the empire- OB1, hah good night. But you can use this similar technique for other screen sizes you know of.
There is no way to determine whether your app is built for iPhone or iPad. Preprocessor
#if
directives are resolved during build. Once your app is built and flagged as Universal, it has to run correctly on both devices. During building nobody knows where it will be installed later and one build can be installed on both.However you may want to do one of these:
Detect device model during runtime.
To do this, use
[[UIDevice currentDevice] model]
and compare toiPhone
,iPod touch
oriPad
strings. This will return you correct device even when running in compatibility mode on iPad (for iPhone-only apps). This can be usefull for usage analytics.Detect user interface idiom during runtime.
This is what everyone checks for, when providing different content for iPhone and iPad. Use
[[UIDevice currentDevice] userInterfaceIdiom]
and compare toUIUserInterfaceIdiomPhone
orUIUserInterfaceIdiomPad
. You may want to make convenience methods like this:Then you can use:
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
Some ideas in the comment section of this blog
http://greensopinion.blogspot.com/2010/04/from-iphone-to-ipad-creating-universal.html
Mostly using
Such as:
Updation for swift:
Couldn't use preprocessor. Make global function as