Conditional code for universal iOS apps

With help from one of my colleagues, I found a simple way of specifying a set of rules for iPad, and a separate set of rules for iPhone/iPod Touch. Apple recommends using user interface idioms, which might look something like this:

#ifdef UI_USER_INTERFACE_IDIOM()
#define IS_IPAD() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#else
#define IS_IPAD() (false)
#endif

usage:
if (IS_IPAD())
{
// foo
}

That’s all well and good, but upper-case code is a little bit scary to me, so I prefer this:

CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.width == 320) {
// Some code for iPhones and iPod Touches
} else {
// Some code for iPads
}

Cool.

2 Responses to Conditional code for universal iOS apps

  1. Kaylee says:

    Exactly where is the facebook like button ?

  2. Refugio says:

    Many thanks for spending some time to explain the terminlogy towards the learners!

Leave a Reply

Your email address will not be published. Required fields are marked *

*