7

How can I get the screen size programmatically in inches(for example iPhone 4, 3.5 inches).

I found a way to do it by detecting the iPhone/iPad model but hard coding is not what I want so I am not looking something like that.

MCKapur
  • 9,127
  • 9
  • 58
  • 101
Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103
  • Why, for the love of Chtulhu, would you want that? Im just kidding, but this requirement is a sure sign that you're on the wrong track. And in inches, even... – katzenhut May 19 '15 at 13:57
  • @katzenhut I cannot remember what it was called but there was a formula to calculate the most optimum video bit rate based on the screen size. So it was for that reason. Now looking back, I'm glad I didn't hard code or used a hard coded library like MCKapur suggested, as since then 5 more iPhones released and many more new iOS devices were released. – Sarp Kaya May 20 '15 at 04:35
  • 1
    @katzenhut: Ruler that displays an inch on the screen that is actually an inch long. – gnasher729 Mar 01 '16 at 15:29

4 Answers4

7

Swift 4 Version for screen

    let scale = UIScreen.main.scale

    let ppi = scale * ((UIDevice.current.userInterfaceIdiom == .pad) ? 132 : 163);

    let width = UIScreen.main.bounds.size.width * scale
    let height = UIScreen.main.bounds.size.height * scale

    let horizontal = width / ppi, vertical = height / ppi;

    let diagonal = sqrt(pow(horizontal, 2) + pow(vertical, 2))
    let screenSize = String(format: "%0.1f", diagonal)
Hitesh Agarwal
  • 1,943
  • 17
  • 21
6

This will find the diagonal screen size of a device:

float scale = [[UIScreen mainScreen] scale];

float ppi = scale * ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? 132 : 163);

float width = ([[UIScreen mainScreen] bounds].size.width * scale);
float height = ([[UIScreen mainScreen] bounds].size.height * scale);

float horizontal = width / ppi, vertical = height / ppi;

float diagonal = sqrt(pow(horizontal, 2) + pow(vertical, 2));

diagonal will contain the diagonal size, in inches, of the screen.

Jack Greenhill
  • 10,240
  • 12
  • 38
  • 70
  • Is this really giving the correct value for all devices ? That way we could findout the screensize of the device, but everyone is telling in forums this is not possible. – mcfly soft Mar 16 '15 at 11:47
  • 3
    Nope iOS devices have a wide range of screens with different PPI counts: 132, 264, 326, and 401. As far as I know you can't get the PPI from a device using the SDK. The only option I see is mapping each individual device name with the appropriate PPI. – psobko Sep 12 '15 at 02:58
  • Use UIScreen nativeScale to get the scale factor for the physical screen. As result will be 401 PPI for a plus version or 458 for X – Evgeny Karpov Jul 12 '18 at 23:12
  • 1
    This is wrong solution. The values are incorrect for some devices. – Alexander Volkov Aug 09 '21 at 11:10
5

I found a nice GitHub project called 'GBDeviceInfo':

if ([GBDeviceInfo deviceDetails].display == GBDeviceDisplayiPhone35Inch) {
    //3.5 inch iphone
}
else if ([GBDeviceInfo deviceDetails].display == GBDeviceDisplayiPhone4Inch) {
    //4 inch iphone
}
else if ([GBDeviceInfo deviceDetails].display == GBDeviceDisplayiPad) {
    //ipad
}

Here 'tis: GBDeviceInfo

MCKapur
  • 9,127
  • 9
  • 58
  • 101
  • 1
    This is no longer accurate, iPhone 6 plus reports a 5.2 inch screen which is actually 5.5 inches as well as a few other incorrect screen sizes – luke Jun 19 '18 at 13:55
0

I found this library, which can be used to get the screen size in inches

https://github.com/detroit-labs/IRLSize

You can get the screen size in inches as below

let screenHeight = UIDevice.current.physicalScreenHeight
let screenWidth = UIDevice.current.physicalScreenWidth
SamB
  • 1,560
  • 1
  • 13
  • 19
  • 2
    Please don't just post some tool / package or library as an answer. At least demonstrate [how it solves the problem](//meta.stackoverflow.com/a/251605) in the answer itself. – 4b0 Aug 20 '21 at 05:36