Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to programmatically detect iPad/iPhone hardware

The reason I need to find out is that on an iPad, a UIPickerView has the same height in landscape orientation as it does in portrait. On an iPhone it is different. The iPad programming guide introduces an "idiom" value to UIDevice:

    UIDevice* thisDevice = [UIDevice currentDevice];     if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)     {         // iPad     }     else     {         // iPhone     } 

which works OK while you're in iPad (3.2) but not iPhone (3.1.3) - so it looks like there also needs to be an ifdef to conditionally compile that check, like:

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200         UIDevice* thisDevice = [UIDevice currentDevice];         if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)         {             // etc.         } #endif 

To me that's starting to look very clumsy. What's a better way?

like image 593
Adam Eberbach Avatar asked May 19 '10 00:05

Adam Eberbach


1 Answers

Checking at runtime (your first way) is completely different from #if at compile time. The preprocessor directives won't give you a universal app.

The preferred way is to use Apple's Macro:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {      // The device is an iPad running iPhone 3.2 or later. } else {      // The device is an iPhone or iPod touch. } 

Use 3.2 as the base SDK (because the macro is not defined pre 3.2), you can target prior OS versions to get it running on the iPhone.

like image 131
Eiko Avatar answered Sep 25 '22 16:09

Eiko