views:

842

answers:

3

Is there an API for checking at runtime whether you are running on an iPhone or an iPad?

One way I can think of would be to use:

[[UIDevice currentDevice] model];

And detect the existence of the string @"iPad" - which seems a bit fragile.

In the 3.2 SDK, I see that UIDevice also has a property which is really what I'm looking for, but doesn't work for pre-3.2 (obviously):

[[UIDevice currentDevice] userInterfaceIdiom]; 

Are there other ways than checking for the existence of @"iPad" for a universal app?

+6  A: 

Checkout UI_USER_INTERFACE_IDIOM

http://developer.apple.com/iphone/library/documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/macro/UI_USER_INTERFACE_IDIOM

Andiih
Gotcha - I can use respondsToSelector:@selector(userInterfaceIdiom) instead of checking for the existence of the string "iPad". Thanks!
Eric
use the macro - later OS's will respond to the selector, but not necessarily be an iPad.
Andiih
Ok, I'm learning. :) My #fail:I've been using the simulator to toggle between iPhone and iPad by switching the Active SDK between 3.2 and 3.1 - in which it no longer compiled when the Active SDK is 3.1. Then I jolted the neurons with some caffeine and plugged the #ifdef UI_USER_INTERFACE_IDIOM around it... Anyway, thanks for the followup Andiih - and if I've just compounded my #fail to something #worsethanfailure with the #ifdef, let me know. :)
Eric
Just for cross-linking's sake: http://stackoverflow.com/questions/2576356/how-does-one-get-ui-user-interface-idiom-to-work-with-iphone-os-sdk-3-2
Yar
A: 
  1. Check for the presence of the userInterfaceIdiom property, usings respondsToSelector:. If it doesn't exist, we are on a pre-3.2 device, thus not an iPad.
  2. If userInterfaceIdiom exists, use it.

Edit: ... which is obviously exactly what the UI_USER_INTERFACE_IDIOM() macro does, so use that instead. :)

calmh