tags:

views:

138

answers:

2

I am writing a universal app for both iphone and ipad. How do I determine if the device is an iPad. I have used this link to determine the iPhone type (3G, 3GS).

http://stackoverflow.com/questions/448162/determine-device-iphone-ipod-touch-with-iphone-sdk

+4  A: 

It is highly recommended that you not do device type detection for determining if the application is running on an iPad, but that you examine either features or the user interface idiom. Many applications that test just for specific device types break when new hardware comes out (which tends to be pretty frequent).

Usually, if you need to determine if an application is running on an iPad, it is because you need to adjust the user interface to match the larger display area of the device. For that, Apple recommends that you check the user interface idiom using code like the following:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    // iPad-specific interface here
}
else
{
    // iPhone and iPod touch interface here
}
Brad Larson
+1  A: 

Brad's solution is absolutely right. If you're building a universal app designed to run on iPhones with older OS along with up-to-date iPads and iPhones, you might want to add this code to catch situations where the idiom is not defined.

// If iPhoneOS is 3.2 or greater then __IPHONE_3_2 will be defined
#ifndef __IPHONE_3_2    

typedef enum {
    UIUserInterfaceIdiomPhone,           // iPhone and iPod touch
    UIUserInterfaceIdiomPad,             // iPad
} UIUserInterfaceIdiom;

#define UI_USER_INTERFACE_IDIOM() UIUserInterfaceIdiomPhone

#endif // ifndef __IPHONE_3_2
Luke
This should only be necessary if the application is being built with an older SDK. If you're building with 3.2+ and targeting 3.x, you should not need the extra code. Seeing as how Apple is now requiring that you build using the 4.0 SDK for submission to the App Store, you shouldn't need this code going forward.
Brad Larson
The function just evaluates to 0 on older OS devices, which causes the equality statement to work just as if it had detected a non-iPad device. Therefore, it works as expected even on older OSs.
Brad Larson
Thanks. This helps. I have SDK 4 installed on my iMAC but my iphone still has 3.1.2. So this will be important. I dont have an iPAD (app uses camera for iPhone and photo library for iPAD). Can I "rent" an iPAD from someplace since I dont want to release the app without testing on a real device.
John Qualis