0

Is there any way to get the device type as a #define? I want to get this for an Universal app and I want to be able to know if it is an iPad, iPhone4 or iPhone5.

Thanks!

1

2 Answers 2

2
#define IS_WIDESCREEN ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
#define IS_IPHONE ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone )
#define IS_IPHONE_5 ( IS_IPHONE && IS_WIDESCREEN )
#define IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
5
  • Awesome! Thanks for the quick reply mientus! I'll try it as soon as I get home. Could you just explain to me why UIScreen and UIDevice would be available to call before runtime? How can the pre-processor be defined using values generated at runtime?
    – DeviArt
    Dec 4, 2012 at 23:46
  • 2
    Don’t compare strings there! The thing is a phone, if it’s of the according UIUserInterfaceIdiom.
    – danyowdee
    Dec 4, 2012 at 23:53
  • 1
    @DeviArt your choices are either to test at runtime or at compile time. If you test at compile time then you won't have a universal application in any meaningful sense.
    – Tommy
    Dec 4, 2012 at 23:54
  • Tommy of course, that makes sense. I guess I just showed my true newb colors.
    – DeviArt
    Dec 4, 2012 at 23:57
  • But note that you can't use those in a #if for conditional compilation. They could just as well be normal function calls, since the values are only available at runtime.
    – Hot Licks
    Dec 4, 2012 at 23:59
2

Counter question:

Why use a macro, when there are methods available?
The macro translates into a method call any way, as you cannot know at compile time what device you’ll be confronted with at run time.

The device you’re running on is an iPad, if it says so:

BOOL isThisAnIPad = [[UIDevice currentDevice] userInterfaceIdiom] == UIInterfaceIdiomPad;

Apart from that, you automatically get the right resources by suffixing them with the correct values in combination with the usual @2x for retina graphics.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.