0

The point of my question is to be able to dynamically include / exclude code depending on which device is used i need some thing link

#if (TARGET_IPHONE_SIMULATOR)

or

`#if (TARGET_OS_IPHONE)

but to specify if device is an ipad or iphone

Mouhamad Lamaa
  • 884
  • 2
  • 12
  • 26
  • possible duplicate of [API to determine whether running on iPhone or iPad](http://stackoverflow.com/questions/2884391/api-to-determine-whether-running-on-iphone-or-ipad) – Vladimir Sep 14 '11 at 15:08

3 Answers3

2

I defined two macros in my _prefix.pch file to make it even easier (and more readable) throughout my code, so you can do:

if (iPad) 

or

if (iPhone)

Here's the code:

#ifndef iPad
    #define iPad    (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#endif
#ifndef iPhone
    #define iPhone  (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad)
#endif
Q8i
  • 1,767
  • 17
  • 25
Javier Soto
  • 4,840
  • 4
  • 26
  • 46
  • is this works for conditional compilation ??? i can use this code to #include "SomeIphoneConditionals.h" or #include "SomeIphoneConditionals.h" ??? – Mouhamad Lamaa Sep 15 '11 at 07:25
  • If you want to have them available in all of your files, put those lines in your _prefix.pch file :) – Javier Soto Sep 15 '11 at 08:46
  • no, i want to include some headers file in a specific classes, also i need to create some methods to be conditionally (example: i want to create a methods to be compiled on ipad and not on iphone) is this possible ? – Mouhamad Lamaa Sep 15 '11 at 08:59
  • 1
    No, that is not possible. Wether an app is running on an iPhone or on an iPad is determined in runtime. The code is the same (that's what universal app means), so you can't use conditional compilation for that. – Javier Soto Sep 15 '11 at 09:29
0

It's not conditional compilation, but:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    // iPhone Code
} else {
    // iPad Code
}

Which is just a macro for UIDevice's -userInterfaceIdiom method.

Matt Wilding
  • 20,115
  • 3
  • 67
  • 95
0

Use UI_USER_INTERFACE_IDIOM()

It returns UIUserInterfaceIdiomPhone or UIUserInterfaceIdiomPad

Matt Wilding
  • 20,115
  • 3
  • 67
  • 95
Youssef
  • 3,582
  • 1
  • 21
  • 28