139

In iPhone App, while running the App on device How to detect the screen resolution of the device on which App is running?

0

7 Answers 7

362
CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

That will give you the entire screen's resolution in points, so it would most typically be 320x480 for iPhones. Even though the iPhone4 has a much larger screen size iOS still gives back 320x480 instead of 640x960. This is mostly because of older applications breaking.

CGFloat screenScale = [[UIScreen mainScreen] scale]; 

This will give you the scale of the screen. For all devices that do not have Retina Displays this will return a 1.0f, while Retina Display devices will give a 2.0f and the iPhone 6 Plus (Retina HD) will give a 3.0f.

Now if you want to get the pixel width & height of the iOS device screen you just need to do one simple thing.

CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale); 

By multiplying by the screen's scale you get the actual pixel resolution.

A good read on the difference between points and pixels in iOS can be read here.

EDIT: (Version for Swift)

let screenBounds = UIScreen.main.bounds let screenScale = UIScreen.main.scale let screenSize = CGSize(width: screenBounds.size.width * screenScale, height: screenBounds.size.height * screenScale) 
Sign up to request clarification or add additional context in comments.

7 Comments

Except that this isn't quite the 'actual pixel' resolution in the case of iPhone 6 Plus. It's the resolution everything is rendered at (except OpenGL) in code, with a 3x scale, but then that is down-sampled internally to the screen's native resolution of 1080 x 1920. One of many good explanations at link
Sadly, this won't give you the "true" dimensions of elements on screen, since Apple's notions of "points" and "scale" are only an approximation. (See specs on iPhone vs iPad vs iPad mini.) Presumably to reduce the number of different combinations that exist. I think iPhone 6 Plus is particularly far off.
Actually 6+ not too far off: height 736 pts / 160 (pt/in) = 4.60" logical height; actual screen height is 4.79"; 5% error. iPad is much farther off: height 1024 pts / 160 (pt/in) = 6.40" logical height; actual screen height is 7.76"; 20% error. iPad mini is OK; it matches original iPhone density. For most purposes, this means one should test iPad software on iPad mini (to make sure it is useable), then simply ignore the fact that most iPads magnify the image by 20% (compared to iPhone or iPad mini).
@RobP so how do you solve this for the iPhone 6 Plus?
@Crashalot not sure what you mean by 'solve this'? It depends on the purpose you have in mind when you obtain screen resolution. As far as programmers are concerned, Jman012's answer is correct and you render into a 1242x2208 or 2208x1242 space. Heck, that's even the resolution we provide launch images at. The fact that the hardware then down samples this image and displays it on a physical screen with a smaller pixel count would be an 'implementation detail' our code shouldn't even be aware of.
|
30

UIScreen class lets you find screen resolution in Points and Pixels.

Screen resolutions is measured in Points or Pixels. It should never be confused with screen size. A smaller screen size can have higher resolution.

UIScreen's 'bounds.width' return rectangular size in Points enter image description here

UIScreen's 'nativeBounds.width' return rectangular size in Pixels.This value is detected as PPI ( Point per inch ). Shows the sharpness & clarity of the Image on a device. enter image description here

You can use UIScreen class to detect all these values.

Swift3

// Normal Screen Bounds - Detect Screen size in Points. let width = UIScreen.main.bounds.width let height = UIScreen.main.bounds.height print("\n width:\(width) \n height:\(height)") // Native Bounds - Detect Screen size in Pixels. let nWidth = UIScreen.main.nativeBounds.width let nHeight = UIScreen.main.nativeBounds.height print("\n Native Width:\(nWidth) \n Native Height:\(nHeight)") 

Console

width:736.0 height:414.0 Native Width:1080.0 Native Height:1920.0 

Swift 2.x

//Normal Bounds - Detect Screen size in Points. let width = UIScreen.mainScreen.bounds.width let height = UIScreen.mainScreen.bounds.height // Native Bounds - Detect Screen size in Pixels. let nWidth = UIScreen.mainScreen.nativeBounds.width let nHeight = UIScreen.mainScreen.nativeBounds.height 

ObjectiveC

// Normal Bounds - Detect Screen size in Points. CGFloat *width = [UIScreen mainScreen].bounds.size.width; CGFloat *height = [UIScreen mainScreen].bounds.size.height; // Native Bounds - Detect Screen size in Pixels. CGFloat *width = [UIScreen mainScreen].nativeBounds.size.width CGFloat *height = [UIScreen mainScreen].nativeBounds.size.width 

Comments

7

Use it in App Delegate: I am using storyboard

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size; //----------------HERE WE SETUP FOR IPHONE 4/4s/iPod---------------------- if(iOSDeviceScreenSize.height == 480){ UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil]; // Instantiate the initial view controller object from the storyboard UIViewController *initialViewController = [iPhone35Storyboard instantiateInitialViewController]; // Instantiate a UIWindow object and initialize it with the screen size of the iOS device self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Set the initial view controller to be the root view controller of the window object self.window.rootViewController = initialViewController; // Set the window object to be the key window and show it [self.window makeKeyAndVisible]; iphone=@"4"; NSLog(@"iPhone 4: %f", iOSDeviceScreenSize.height); } //----------------HERE WE SETUP FOR IPHONE 5---------------------- if(iOSDeviceScreenSize.height == 568){ // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4 UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:@"iPhone5" bundle:nil]; // Instantiate the initial view controller object from the storyboard UIViewController *initialViewController = [iPhone4Storyboard instantiateInitialViewController]; // Instantiate a UIWindow object and initialize it with the screen size of the iOS device self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Set the initial view controller to be the root view controller of the window object self.window.rootViewController = initialViewController; // Set the window object to be the key window and show it [self.window makeKeyAndVisible]; NSLog(@"iPhone 5: %f", iOSDeviceScreenSize.height); iphone=@"5"; } } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { // NSLog(@"wqweqe"); storyboard = [UIStoryboard storyboardWithName:@"iPad" bundle:nil]; } return YES; } 

Comments

5

For iOS 8 we can just use this [UIScreen mainScreen].nativeBounds , like that:

- (NSInteger)resolutionX { return CGRectGetWidth([UIScreen mainScreen].nativeBounds); } - (NSInteger)resolutionY { return CGRectGetHeight([UIScreen mainScreen].nativeBounds); } 

Comments

4

See the UIScreen Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html

if([[UIScreen mainScreen] respondsToSelector:NSSelectorFromString(@"scale")]) { if ([[UIScreen mainScreen] scale] < 1.1) NSLog(@"Standard Resolution Device"); if ([[UIScreen mainScreen] scale] > 1.9) NSLog(@"High Resolution Device"); } 

2 Comments

thanx for reply if i am Putting it in NSLog(@"%d",[[UIScreen mainScreen] scale]); it gives 0 ...... and NSLog(@"%@",[[UIScreen mainScreen] scale]); it gives nil Pls let me know how to get screen resolution or how to test whether it is giving correct resolution while running it on simulator
try NSLog(@"%f",[[UIScreen mainScreen] scale]);
0

Use this code it will help for getting any type of device's screen resolution

 [[UIScreen mainScreen] bounds].size.height [[UIScreen mainScreen] bounds].size.width 

Comments

0

If your goal is to get the model resolution type and not the resolution values themeselfs, this Swift solution might be helpful:

import UIKit @objc(IphoneModelScreenSize) public class IphoneModelScreenSize: NSObject { // MARK: Enums public enum IphoneModelScreenSize: Int { case notAnIphone = 0, twoThreeOrFour = 1, se = 2, sixSevenOrEight = 3, plus = 4, elevenXorXS = 5, elevenProMaxOrXsMax = 6 } // MARK: Class properties public class func screenSize() -> IphoneModelScreenSize { let bounds = UIScreen.main.bounds let screenWidth = bounds.size.width let screenHeight = bounds.size.height switch (screenWidth, screenHeight) { case (320.0, 480.0): return .twoThreeOrFour case (320.0, 568.0): return .se case (375.0, 667.0): return .sixSevenOrEight case (414.0, 736.0): return .plus case (375.0, 812.0): return .elevenXorXS case (414.0, 896.0): return .elevenProMaxOrXsMax default: return .notAnIphone } } public class func screenSizeStringValue() -> String { return screenSizeEnumToString(screenSize()) } // MARK: Private properties private class func screenSizeEnumToString(_ screenSize: IphoneModelScreenSize) -> String { var screenSizeAsString: String switch screenSize { case .notAnIphone: screenSizeAsString = "Not an Iphone" case .twoThreeOrFour: screenSizeAsString = "2G, 3G, 3GS, 4 or 4s" case .se: screenSizeAsString = "5, 5s, 5c or SE" case .sixSevenOrEight: screenSizeAsString = "6, 6s, 7 or 8" case .plus: screenSizeAsString = "6+, 6s+, 7+ or 8+" case .elevenXorXS: screenSizeAsString = "11 Pro, X or Xs" case .elevenProMaxOrXsMax: screenSizeAsString = "11, Xr, 11 Pro Max or Xs Max" } return screenSizeAsString } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.