ios - How to detect iPhone 5 (widescreen devices)?

ID : 10261

viewed : 21

Tags : iphoneiosobjective-cscreendeviceiphone

Top 5 Answer for ios - How to detect iPhone 5 (widescreen devices)?

vote vote

92

First of all, you shouldn't rebuild all your views to fit a new screen, nor use different views for different screen sizes.

Use the auto-resizing capabilities of iOS, so your views can adjust, and adapt any screen size.

That's not very hard, read some documentation about that. It will save you a lot of time.

iOS 6 also offers new features about this.
Be sure to read the iOS 6 API changelog on Apple Developer website.
And check the new iOS 6 AutoLayout capabilities.

That said, if you really need to detect the iPhone 5, you can simply rely on the screen size.

[ [ UIScreen mainScreen ] bounds ].size.height 

The iPhone 5's screen has a height of 568.
You can imagine a macro, to simplify all of this:

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON ) 

The use of fabs with the epsilon is here to prevent precision errors, when comparing floating points, as pointed in the comments by H2CO3.

So from now on you can use it in standard if/else statements:

if( IS_IPHONE_5 ) {} else {} 

Edit - Better detection

As stated by some people, this does only detect a widescreen, not an actual iPhone 5.

Next versions of the iPod touch will maybe also have such a screen, so we may use another set of macros.

Let's rename the original macro IS_WIDESCREEN:

#define IS_WIDESCREEN ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON ) 

And let's add model detection macros:

#define IS_IPHONE ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPhone" ] ) #define IS_IPOD   ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPod touch" ] ) 

This way, we can ensure we have an iPhone model AND a widescreen, and we can redefine the IS_IPHONE_5 macro:

#define IS_IPHONE_5 ( IS_IPHONE && IS_WIDESCREEN ) 

Also note that, as stated by @LearnCocos2D, this macros won't work if the application is not optimised for the iPhone 5 screen (missing the Default-568h@2x.png image), as the screen size will still be 320x480 in such a case.

I don't think this may be an issue, as I don't see why we would want to detect an iPhone 5 in a non-optimized app.

IMPORTANT - iOS 8 support

On iOS 8, the bounds property of the UIScreen class now reflects the device orientation.
So obviously, the previous code won't work out of the box.

In order to fix this, you can simply use the new nativeBounds property, instead of bounds, as it won't change with the orientation, and as it's based on a portrait-up mode.
Note that dimensions of nativeBounds is measured in pixels, so for an iPhone 5 the height will be 1136 instead of 568.

If you're also targeting iOS 7 or lower, be sure to use feature detection, as calling nativeBounds prior to iOS 8 will crash your app:

if( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) {     /* Detect using nativeBounds - iOS 8 and greater */ } else {     /* Detect using bounds - iOS 7 and lower */ } 

You can adapt the previous macros the following way:

#define IS_WIDESCREEN_IOS7 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON ) #define IS_WIDESCREEN_IOS8 ( fabs( ( double )[ [ UIScreen mainScreen ] nativeBounds ].size.height - ( double )1136 ) < DBL_EPSILON ) #define IS_WIDESCREEN      ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_WIDESCREEN_IOS8 : IS_WIDESCREEN_IOS7 ) 

And obviously, if you need to detect an iPhone 6 or 6 Plus, use the corresponding screen sizes.

vote vote

90

Tested and designed for any combination of SDK and OS:

Swift

Added iPad types. iPad 2 and iPad mini are non-retina iPads. While iPad Mini 2 & above, iPad 3, 4, iPad Air, Air 2, Air 3, and iPad Pro 9.7 have same logical resolution of 1024. iPad Pro has maxLength of 1366. Reference

import UIKit  public enum DisplayType {     case unknown     case iphone4     case iphone5     case iphone6     case iphone6plus     case iPadNonRetina     case iPad     case iPadProBig     static let iphone7 = iphone6     static let iphone7plus = iphone6plus }  public final class Display {     class var width:CGFloat { return UIScreen.main.bounds.size.width }     class var height:CGFloat { return UIScreen.main.bounds.size.height }     class var maxLength:CGFloat { return max(width, height) }     class var minLength:CGFloat { return min(width, height) }     class var zoomed:Bool { return UIScreen.main.nativeScale >= UIScreen.main.scale }     class var retina:Bool { return UIScreen.main.scale >= 2.0 }     class var phone:Bool { return UIDevice.current.userInterfaceIdiom == .phone }     class var pad:Bool { return UIDevice.current.userInterfaceIdiom == .pad }     class var carplay:Bool { return UIDevice.current.userInterfaceIdiom == .carPlay }     class var tv:Bool { return UIDevice.current.userInterfaceIdiom == .tv }     class var typeIsLike:DisplayType {         if phone && maxLength < 568 {             return .iphone4         }         else if phone && maxLength == 568 {                 return .iphone5         }         else if phone && maxLength == 667 {             return .iphone6         }         else if phone && maxLength == 736 {             return .iphone6plus         }         else if pad && !retina {             return .iPadNonRetina         }         else if pad && retina && maxLength == 1024 {             return .iPad         }         else if pad && maxLength == 1366 {             return .iPadProBig         }         return .unknown     } } 

See it in action https://gist.github.com/hfossli/bc93d924649de881ee2882457f14e346

Note: If e.g. iPhone 6 is in zoomed mode the UI is a zoomed up version of iPhone 5. These functions is not determining device type, but display mode thus iPhone 5 is the desired result in this example.

Objective-C

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) #define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)  #define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width) #define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height) #define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT)) #define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT)) #define IS_ZOOMED (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)  #define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0) #define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0) #define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0) #define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0) 

Usage: http://pastie.org/9687735

Note: If e.g. iPhone 6 is in zoomed mode the UI is a zoomed up version of iPhone 5. These functions is not determining device type, but display mode thus iPhone 5 is the desired result in this example.

vote vote

79

Really simple solution

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {     CGSize result = [[UIScreen mainScreen] bounds].size;     if(result.height == 480)     {         // iPhone Classic     }     if(result.height == 568)     {         // iPhone 5     } } 
vote vote

68

We now need to account for iPhone 6 and 6Plus screen sizes. Here's an updated answer

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {     //its iPhone. Find out which one?      CGSize result = [[UIScreen mainScreen] bounds].size;     if(result.height == 480)     {         // iPhone Classic     }     else if(result.height == 568)     {         // iPhone 5     }     else if(result.height == 667)     {         // iPhone 6     }    else if(result.height == 736)     {         // iPhone 6 Plus     } } else {      //its iPad } 

Some useful info

iPhone 6 Plus   736x414 points  2208x1242 pixels    3x scale    1920x1080 physical pixels   401 physical ppi    5.5" iPhone 6        667x375 points  1334x750 pixels     2x scale    1334x750 physical pixels    326 physical ppi    4.7" iPhone 5        568x320 points  1136x640 pixels     2x scale    1136x640 physical pixels    326 physical ppi    4.0" iPhone 4        480x320 points  960x640 pixels      2x scale    960x640 physical pixels     326 physical ppi    3.5" iPhone 3GS      480x320 points  480x320 pixels      1x scale    480x320 physical pixels     163 physical ppi    3.5" 
vote vote

52

I've taken the liberty to put the macro by Macmade into a C function, and name it properly because it detects widescreen availability and NOT necessarily the iPhone 5.

The macro also doesn't detect running on an iPhone 5 in case where the project doesn't include the Default-568h@2x.png. Without the new Default image, the iPhone 5 will report a regular 480x320 screen size (in points). So the check isn't just for widescreen availability but for widescreen mode being enabled as well.

BOOL isWidescreenEnabled() {     return (BOOL)(fabs((double)[UIScreen mainScreen].bounds.size.height -                                                 (double)568) < DBL_EPSILON); } 

Top 3 video Explaining ios - How to detect iPhone 5 (widescreen devices)?

Related QUESTION?