Identify different iOS devices in coding? [closed]

The way I determine what iOS device I am running on so we can change layout based on the iDevices size such as iPad and iPhone is like.

   // iPhone 5 (iPhone 4")
   #define IS_PHONEPOD5() ([UIScreen mainScreen].bounds.size.height == 568.0f && [UIScreen mainScreen].scale == 2.f && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

To get iPhone 5 you could also do

  // iPhone 5 alternative
  #define IS_PHONEPOD5() ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

  // iPad
  #define IS_IPAD() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

  // iPhone 4 (iPhone 3.5")
  #define IS_IPHONE() (UI_USER_INTERFACE_IDIOM() == UIUserIntefaceIdiomPhone)

I add these into my xxxx-Prefix.pch so that I can use it throughout my project without having to do an #import or #include. I am not saying this is the best way but this way works perfectly fine for me.

If you set these variables you can then just use if statements like

  if(IS_IPAD()) {
      // Do something if iPad
  } else if(IS_IPHONEPOD5()) {
      // Do something if iPhone 5 or iPod 5
  } else {
     // Do something for everything else (This is what I do) but you could 
     // replace the 'else' with 'else if(IS_IPHONE())'


There are other ways of doing it though. Such as one developer has written some code that extends UIDevice this can be found

  [[UIDevice currentDevice] platformType]   // ex: UIDevice4GiPhone
  [[UIDevice currentDevice] platformString] // ex: @"iPhone 4G"

This will allow you to detect whether the iDevice is an iPhone 3, 3GS, 4 and so on.

If you have any questions please just ask in the comments and I will improve my answer to include. Hope it helps if it has here is a google search for determining iDevice

Leave a Comment