The best way to programmatically identify iPad / iPhone hardware

The reason I need to find out is that on the iPad, UIPickerView has the same height in landscape orientation as in portrait. On the iPhone, this is different. The iPad Programming Guide introduces the idiom value for the UIDevice:

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

which works fine when you are on an iPad (3.2) but not an iPhone (3.1.3) - so it looks like there should also be an ifdef there to conditionally compile this check, for example:

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

It seems very awkward to me. What is the best way?

+45
iphone ipad orientation
May 19 '10 at 12:41 a.m.
source share
9 answers

I am answering this now (and at this late date), because many of the existing answers are quite old, and most Up Voted actually seems incorrect according to current Apple docs (iOS 8.1, 2015)!

To prove your point, this is a comment from the Apples header file (always look at the source and Apple headers):

 /*The UI_USER_INTERFACE_IDIOM() macro is provided for use when deploying to a version of the iOS less than 3.2. If the earliest version of iPhone/iOS that you will be deploying for is 3.2 or greater, you may use -[UIDevice userInterfaceIdiom] directly.*/ 

Therefore, at present, APPLE recommends a method for detecting iPhone vs iPad, as follows:

1) In iOS PRIOR versions prior to 3.2, use the macro provided by Apple:

 // for iPhone use UIUserInterfaceIdiomPhone if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 

2) In versions of iOS 3.2 or later, use the property in [UIDevice currentDevice]:

 // for iPhone use UIUserInterfaceIdiomPhone if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) 
+24
Apr 07 '15 at 10:12
source share

Check at runtime (your first way) is completely different from #if at compile time. Preprocessor directives do not give you a universal application.

The preferred way is to use Apple 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 before 3.2), you can configure the target OS versions to run on the iPhone.

+61
Jun 02 2018-10-02T00:
source share

I like my isPad () function. The same code, but keep it out of sight and in only one place.

+17
May 19 '10 at
source share

My solution (works with 3.2 +):

 #define IS_IPHONE (!IS_IPAD) #define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone) 

then

 if (IS_IPAD) // do something 

or

 if (IS_IPHONE) // do something else 
+14
Jul 22 2018-11-11T00:
source share

In Swift, use the userInterfaceIdiom instance property of as-

 if UIDevice.current.userInterfaceIdiom == .phone { print("iPhone") } 

& For other devices -

  switch UIDevice.current.userInterfaceIdiom { case .pad: print("iPad") case .phone: print("iPhone") case .tv: print("TV") case .carPlay: print("carPlay") default: break; } 
+1
Dec 07 '17 at 7:14
source share

Put this method in your application delegate so that you can call it anywhere using [[[UIApplication sharedApplication] delegate] isPad]

 -(BOOL)isPad { BOOL isPad; NSRange range = [[[UIDevice currentDevice] model] rangeOfString:@"iPad"]; if(range.location==NSNotFound) { isPad=NO; } else { isPad=YES; } return isPad; } 
0
Jul 05 '10 at 10:44
source share

If you use functions that do not support backward compatibility, I have found for me the best way to create #define in a precompiled header. Example:

 #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_3_2 #define USING_4_X #endif 

Then in your code you can do this:

 BOOL exists = NO; #ifdef USING_4_X exists = [SomeObject someMethod:[url lastPathComponent]]; #else exists = [SomeObject someMethod:[[url path] lastPathComponent]]; #endif 
0
Nov 05 '10 at 3:20
source share

If 1- you have already installed the application on your device, 2- you change the settings of your assembly as a universal application, 3- install the application on your device on top of an existing application (without deleting the previous one)

You may find that the iPhone / iPad detection solutions offered here are not working. First, uninstall the iPad / iPhone only application and install it on your device.

0
Sep 22 '13 at 17:32
source share
 BOOL isIpad() { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { return YES; } return NO; } 
0
May 15 '14 at 2:51
source share



All Articles