Detecting iPhone 6/6 + screen sizes in dot values

Given the recently announced iPhone 6 screen sizes:

iPhone 6: 1334h * 750w @2x (in points: 667h * 375w) iPhone 6+: 1920 * 1080 @3x (in points: 640h * 360w) 

I was wondering if there is code that allows me to determine the size of the screen on which the user device is located, so that I can configure the size of UIImages and other materials accordingly with the user device.

So far I have used the following:

 - (NSString *) platform{ size_t size; sysctlbyname("hw.machine", NULL, &size, NULL, 0); char *machine = malloc(size); sysctlbyname("hw.machine", machine, &size, NULL, 0); NSString *platform = [NSString stringWithUTF8String:machine]; free(machine); return platform; } - (NSString *) platformString{ NSString *platform = [self platform]; if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G"; if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS"; if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4"; if ([platform isEqualToString:@"iPhone3,3"]) return @"Verizon iPhone 4"; if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S"; if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (GSM)"; if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (GSM)"; if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (GSM+CDMA)"; if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (GSM)"; if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (GSM+CDMA)"; if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G"; if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G"; if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G"; if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G"; if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G"; if ([platform isEqualToString:@"iPad1,1"]) return @"iPad"; if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)"; if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)"; if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)"; if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2 (WiFi)"; if ([platform isEqualToString:@"iPad2,5"]) return @"iPad Mini (WiFi)"; if ([platform isEqualToString:@"iPad2,6"]) return @"iPad Mini (GSM)"; if ([platform isEqualToString:@"iPad2,7"]) return @"iPad Mini (GSM+CDMA)"; if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3 (WiFi)"; if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3 (GSM+CDMA)"; if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3 (GSM)"; if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)"; if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4 (GSM)"; if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4 (GSM+CDMA)"; if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air (WiFi)"; if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air (Cellular)"; if ([platform isEqualToString:@"iPad4,4"]) return @"iPad mini 2G (WiFi)"; if ([platform isEqualToString:@"iPad4,5"]) return @"iPad mini 2G (Cellular)"; if ([platform isEqualToString:@"i386"]) return @"Simulator"; if ([platform isEqualToString:@"x86_64"]) return @"Simulator"; return platform; } 

As such, should we assume that iPhone7,1 and iPhone7,2 are iPhone 6, and iPhone7,3 and iPhone7.4 are pluses? If anyone has a more specific way of saying this will be great, thanks.!

+57
ios iphone ios8
Sep 10 '14 at 1:41
source share
16 answers

The first screen will be the screen of the device. Please note that before sending, you need to add launch images for new phones, otherwise the application will be launched in Zoomed Mode for older applications: Here is the code that I used to check this. Note. This only works with iOS version 8 and higher :

 UIScreen *mainScreen = [UIScreen mainScreen]; NSLog(@"Screen bounds: %@, Screen resolution: %@, scale: %f, nativeScale: %f", NSStringFromCGRect(mainScreen.bounds), mainScreen.coordinateSpace, mainScreen.scale, mainScreen.nativeScale); 

Code for detecting iPhone 6 Plus:

 #define IS_PAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define IS_PHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) -(BOOL)iPhone6PlusDevice{ if (!IS_PHONE) return NO; if ([UIScreen mainScreen].scale > 2.9) return YES; // Scale is only 3 when not in scaled mode for iPhone 6 Plus return NO; } 

or

 -(BOOL) iPhone6PlusUnZoomed{ if ([self iPhone6PlusDevice]){ if ([UIScreen mainScreen].bounds.size.height > 720.0) return YES; // Height is 736, but 667 when zoomed. } return NO; } 

Note. If you are testing the iPhone 6 Plus to customize the user interface, then do not rely on .nativeScale because the simulator and the actual device give different results. Due to the comment below. Scale is CGFloat, and therefore the code should not check for equality, since some float values ​​can never be equal.




After adding boot images for the new iPhone6 ​​and 6Plus, the sizes change. They scale old applications to fit the screen.

Size for iPhone 6 Plus and iPhone 6S Plus with @ 3x scaling (Apple name: Retina HD 5.5 ), coordinate space: <strong> 414 x 736 and 1242 x 2208 pixels, 401 dpi, physical screen size 2.7 x 4 , 8 inches or 68 x 122 mm :

 Screen bounds: {{0, 0}, {414, 736}}, Screen resolution: <UIScreen: 0x7f97fad330b0; bounds = {{0, 0}, {414, 736}}; mode = <UIScreenMode: 0x7f97fae1ce00; size = 1242.000000 x 2208.000000>>, scale: 3.000000, nativeScale: 3.000000 



Size for iPhone 6 and iPhone 6S with @ 2x scaling (Apple name: Retina HD 4.7 ), coordinate space: 375 x 667 and 750 x 1334 pixels, 326 dpi, physical screen size - 2.3 x 4.1 inches or 58 x 104 mm :

 Screen bounds: {{0, 0}, {375, 667}}, Screen resolution: <UIScreen: 0x7fa01b5182d0; bounds = {{0, 0}, {375, 667}}; mode = <UIScreenMode: 0x7fa01b711760; size = 750.000000 x 1334.000000>>, scale: 2.000000, nativeScale: 2.000000 

And iPhone 5 for comparison is 640 x 1136, iPhone 4 640 x 960.

Note. Download LaunchImages, otherwise the application will start scaling and not display the correct scaling or screen size.

Comparing iPhone 6 and 6 Plus

+98
Sep 10 '14 at 1:46
source share

If you prefer macros, these are the ones you can use to distinguish between iPhone models. They are based on point values.

 #define IS_IPHONE_4 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)480) < DBL_EPSILON) #define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)568) < DBL_EPSILON) #define IS_IPHONE_6 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)667) < DBL_EPSILON) #define IS_IPHONE_6_PLUS (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)736) < DBL_EPSILON) 
+32
Sep 28 '14 at 9:29
source share

I use the following code to determine which device is working (it is a little quick and dirty, but it does the trick)

 if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ){ CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height; CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width; if( screenHeight < screenWidth ){ screenHeight = screenWidth; } if( screenHeight > 480 && screenHeight < 667 ){ NSLog(@"iPhone 5/5s"); } else if ( screenHeight > 480 && screenHeight < 736 ){ NSLog(@"iPhone 6"); } else if ( screenHeight > 480 ){ NSLog(@"iPhone 6 Plus"); } else { NSLog(@"iPhone 4/4s"); } } 

(This only works when the iPhone 6/6 Plus is turned on by adding appropriate screensavers)

+17
Oct. 16 '14 at 9:21
source share

On a physical device, the main screen limitations of the iPhone 6 Plus are 2208x1242, and nativeBounds is 1920x1080. There is hardware scaling for resizing to a physical display.

In the simulator, the main screen limitations of the iPhone 6 Plus and nativeBounds are 2208x1242.

In other words ... Videos, OpenGL and other things based on CALayers that deal with pixels will deal with the real framebuffer 1920x1080 on the device (or 2208x1242 on the sim). Things regarding points in UIKit will deal with 2208x1242 (x3) boundaries and scale as needed on the device.

The simulator does not have access to the same equipment that does the scaling on the device, and there is no use to simulate it in software, since they will give different results than the hardware. Thus, it makes sense to set the nativeBounds of the main screen of the simulated device to the borders of the main screen of the physical device.

iOS 8 added an API to UIScreen (nativeScale and nativeBounds) so that the developer can determine the resolution of the CAD file corresponding to UIScreen.

+5
Sep 10 '14 at 6:57
source share

Check the updated wiki list, there I got 7.2 for iPhone 6 and 7.1 for iPhone 6 plus.

+5
Sep 15 '14 at 7:01
source share

You can discover iPhone 6 Plus based on its own scale using this macro:

 #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) #define IS_IPHONE_6PLUS (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f) 
+4
Sep 10 '14 at 16:29
source share

Hannes Sverrisson's answer is almost correct. The iPhone 6 coordinate system is actually more than 5 seconds. Using his code:

 UIScreen *mainScreen = [UIScreen mainScreen]; NSLog(@"Screen bounds: %@, Screen resolution: %@, scale: %f, nativeScale: %f", NSStringFromCGRect(mainScreen.bounds), mainScreen.coordinateSpace, mainScreen.scale, mainScreen.nativeScale); 

The coordinate system for applications that provide the correct launch image:

Size for iPhone 6 (Retina HD 4.7) with @ 2x scaling, coordinate space: 375 x 667 and 750 x 1334 actual points:

 Screen bounds: {{0, 0}, {375, 667}}, Screen resolution: <UIScreen: 0x7fa01b5182d0; bounds = {{0, 0}, {375, 667}}; mode = <UIScreenMode: 0x7fa01b711760; size = 750.000000 x 1334.000000>>, scale: 2.000000, nativeScale: 2.000000 

Size for iPhone 6 Plus (Retina HD 5.5) with @ 3x scaling, coordinate space: 414 x 736 and 1242 x 2208 actual points:

 Screen bounds: {{0, 0}, {414, 736}}, Screen resolution: <UIScreen: 0x7f97fad330b0; bounds = {{0, 0}, {414, 736}}; mode = <UIScreenMode: 0x7f97fae1ce00; size = 1242.000000 x 2208.000000>>, scale: 3.000000, nativeScale: 3.000000 
+4
Sep 10 '14 at 22:21
source share

This is what I use in my application with iOS 8:

 window=[[[UIApplication sharedApplication] windows] firstObject]; NSLog(@"screenHeight=%f width=%f",window.frame.size.height,window.frame.size.width); if (window.frame.size.height == 480) { do stuff here... } 

Prior to Xcode6 / iOS 8, I used this, but the borders of the screen do not work properly with a resizable simulator, or at least in beta versions of Xcode6 ...

 CGRect screenBounds=[[UIScreen mainScreen] bounds]; if (screenBounds.size.height >= 568) { do stuff here... } 
+3
Sep 10 '14 at 1:50
source share

I had to find the iPhone 6 Plus in an application built on iOS 7. Since nativeScale is not available on [UIScreen mainScreen], I tried using the scale of [UIScreen mainScreen]], but it just returned 2.0. So I came up with this solution to detect iPhone 6 Plus on iOS 7 (should also work on iOS 8):

 -(BOOL)iPhone6Plus{ BOOL isiPhone6Plus = NO; SEL selector = NSSelectorFromString(@"scale"); if ([[UIScreen mainScreen] respondsToSelector:selector]) { NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: [[[UIScreen mainScreen] class] instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:[UIScreen mainScreen]]; [invocation invoke]; float returnValue; [invocation getReturnValue:&returnValue]; if (returnValue == 3.0) { isiPhone6Plus = YES; } NSLog(@"ScaleFactor %1.2f", returnValue); } return isiPhone6Plus; 

}

The interesting part of this code is that if I use NSInvocation, the return value of the scale selector will be 3.0. Calling this method directly on iOS 7 returns 2.0.

+3
Sep 24 '14 at 6:21
source share

All three devices have (to a large extent) the same number of dots per inch. Thus, your images will automatically have the same physical size.

Use [[UIScreen mainScreen] bounds] to get the total number of dots on the screen. Divide by 163 to get an approximate size in inches if you really want it.

Please note that 6+ does not return 1080p because it does not display a 1080p buffer. It ensures that the output is approximately 160 dpi using assets @ 3x.

No need to guess.

eg. if you write this code:

 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 163, 163)]; view.backgroundColor = [UIColor redColor]; [self.view addSubview:view]; 

You’ll get a view that is about the same physical value - one square inch - on all iOS devices.

Apple has already done the hard work, so you don't need to.

+2
Sep 10 '14 at 1:50
source share

for me it works for me

 if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){ UIStoryboard *storyBoard; CGSize result = [[UIScreen mainScreen] bounds].size; CGFloat scale = [UIScreen mainScreen].scale; result = CGSizeMake(result.width * scale, result.height * scale); if(result.height == 1136){ storyBoard = [UIStoryboard storyboardWithName:@"Main_iPhone_5" bundle:nil]; UIViewController *initViewController = [storyBoard instantiateInitialViewController]; [self.window setRootViewController:initViewController]; } else if(result.height == 1334){ storyBoard = [UIStoryboard storyboardWithName:@"Main_iPhone_6" bundle:nil]; UIViewController *initViewController = [storyBoard instantiateInitialViewController]; [self.window setRootViewController:initViewController]; } else if(result.height == 2208){ storyBoard = [UIStoryboard storyboardWithName:@"Main_iPhone_6_plus" bundle:nil]; UIViewController *initViewController = [storyBoard instantiateInitialViewController]; [self.window setRootViewController:initViewController]; } else if(result.height == 960){ storyBoard = [UIStoryboard storyboardWithName:@"Main_iPhone_4" bundle:nil]; UIViewController *initViewController = [storyBoard instantiateInitialViewController]; [self.window setRootViewController:initViewController]; } } else { UIStoryboard *storyBoard; storyBoard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil]; UIViewController *initViewController = [storyBoard instantiateInitialViewController]; [self.window setRootViewController:initViewController]; } 
+2
Oct 17 '14 at 20:14
source share

Here is the updated source code that you use this .

Added iPhone 6 and iPhone 6 Plus models.

+1
Sep 18 '14 at 4:42 on
source share

this is guaranteed to compile in xcode 5 (xocde 6 is still fuzzy at this point, and you cannot send ipa to connect itunes to approve the app store using the beta version of the software that now has xcode 6)

thing xcode 5 does not recognize the nativeScale selector .. so you can do this at runtime:

 + (BOOL)isIphone6Plus { SEL selector = NSSelectorFromString(@"nativeScale"); if ([[UIScreen mainScreen] respondsToSelector:selector]) { NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: [[[UIScreen mainScreen] class] instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:[UIScreen mainScreen]]; [invocation invoke]; float returnValue; [invocation getReturnValue:&returnValue]; NSLog(@"::: this is native scale %f", returnValue); return (returnValue == 3.0f); } else { // iphone 6 plus come prepackaged with iOS8.. // so if the phone doesn't know what nativeScale means // it not an iphone6plus phone return NO; } } 
+1
Oct 15 '14 at 7:35
source share

The interesting thing to remember when reading screen sizes on my iPhone 6 Plus was that when you have the “Zoomed” mode set, it will show up as the height of the iPhone 6 (667), and when you set it to “Standard” will be displayed as (736). It doesn’t matter, but if you specifically wanted to know the type of device for any reason (possibly Reporting), this may deceive you.

See this .

+1
May 27 '15 at 13:53
source share

One of the main reasons why the above answers are not taken into account is that in iOS7 and below, when you check [[UIScreen mainScreen] bounds] for the borders of the screen, it always displays the width and height as the same no matter what is the orientation of the phone inch. So if this is iPhone5 in landscape mode, it will still display a width of 320 and a height of 568. In iOS8, this has changed, now if the same iPhone5 is in the landscape, it will display a width of 568 and a height like 320. The following are the methods that explain this:

 + (BOOL) deviceHasFourInchScreen { return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:2.0 height:568.0]; } + (BOOL) deviceHasFourPointSevenInchScreen { return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:2.0 height:667.0]; } + (BOOL) deviceHasFivePointFiveInchScreen { return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:3.0 height:736.0]; } + (BOOL) deviceHasScreenWithIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom scale:(CGFloat)scale height:(CGFloat)height { CGRect mainScreenBounds = [[UIScreen mainScreen] bounds]; CGFloat mainScreenHeight; if ([OperatingSystemVersion operatingSystemVersionLessThan:@"8.0"]) { mainScreenHeight = mainScreenBounds.size.height; } else { mainScreenHeight = (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) ? mainScreenBounds.size.width : mainScreenBounds.size.height; } if ([[UIDevice currentDevice] userInterfaceIdiom] == userInterfaceIdiom && [[UIScreen mainScreen] scale] == scale && mainScreenHeight == height) { return YES; } else { return NO; } } 

The following methods of the OperatingSystem class are also listed:

 + (NSString *) currentOperatingSystemVersion { return [[UIDevice currentDevice] systemVersion]; } + (BOOL) operatingSystemVersionLessThanOrEqualTo:(NSString *) operatingSystemVersionToCompare { return ([[self currentOperatingSystemVersion] compare: operatingSystemVersionToCompare options:NSNumericSearch] != NSOrderedDescending); } 
0
Sep 27 '14 at 22:07
source share

Swift 4

 if(kSize.width == 320){ //iphone se }else if(kSize.width == 375 && kSize.height == 667){ //iphone 7 / 8 }else if(kSize.width == 375){ //iphone x }else if(kSize.width == 414){ //iphone 7 plus/ 8 plus } 

kSize - CGSize screen

let kSize = UIScreen.main.bounds.size

0
Jul 10 '18 at 11:50
source share



All Articles