1

I've looked a lot of post about iOS how to retrieve the real size. I found such solution but I think it doesn't work correctly for all devices.

+(float) getRealSize {
    float scale = 1;
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        scale = [[UIScreen mainScreen] scale];
    }
    float dpi;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        dpi = 132 * scale;
    } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        dpi = 163 * scale;
    } else {
        dpi = 160 * scale;
    }
    CGRect originalFrame = [[UIScreen mainScreen] bounds];
    float w = originalFrame.size.width;
    float h = originalFrame.size.height;
    return dpi * sqrtf(w*w + h*h);
}

How to update such method to support all devices? I want DPI (PPI) or inches. Or, may be, is there any library to help.

To prevent closing as dublicated: there is no proper solutions in stackoverflow. For, instance, iOS get physical screen size programmatically? Getting the physical screen size in inches for iPhone How to get the screen width and height in iOS? etc.,etc.,etc... no solution now.

Community
  • 1
  • 1
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • what do you mean by real size ? device size is different than screen size! When you say device size, you can get those from apple specifications. I think you can even get the screen size from the apple site. What;s the point of calculating them programmatically ? – Teja Nandamuri Jun 08 '16 at 15:07
  • Of course i need screen real size in inches – Vyacheslav Jun 08 '16 at 15:10
  • The first link you claim has no proper solution gives multiple solutions for your issue. – dan Jun 08 '16 at 15:12
  • you already posted answer to your quiestion. Pls add details to your question explaining what worked and what not instead of saying no proper solution. – Teja Nandamuri Jun 08 '16 at 15:12
  • @dan, so, what answer in the URL can help to me? – Vyacheslav Jun 08 '16 at 15:15
  • There is no API in iOS to get this. Use this and make sure to update when there are new devices: https://github.com/lmirosevic/GBDeviceInfo – Lou Franco Jun 08 '16 at 15:16
  • @TejaNandamuri, the code i wrote, works only for old devices – Vyacheslav Jun 08 '16 at 15:16
  • @LouFranco,will it be approved by Apple? – Vyacheslav Jun 08 '16 at 15:18
  • Don't include the jailbreak pod. I don't see why it wouldn't be -- if you are worried, just copy the info out of: https://github.com/lmirosevic/GBDeviceInfo/blob/master/GBDeviceInfo/GBDeviceInfo_iOS.m -- it's just a list of info about the device -- nothing crazy – Lou Franco Jun 08 '16 at 15:20
  • 1
    Ignore your idea of finding an actual "size" for a device; what are you attempting to achieve? If you are attempting any type of interface scaling then this is unlikely to work due to multitasking on an iPhone X+ or an iPad causing an app to not have the full screen. Instead you should use `size classes` and `autolayout`. – Robotic Cat Jun 08 '16 at 15:24
  • @RoboticCat, ihave to scale element of UI depend on real size of device – Vyacheslav Jun 08 '16 at 15:29
  • Robotic: I use a “ruler” app. Shows a three inch or four inch ruler on the screen. That should match a real ruler. – gnasher729 Aug 20 '21 at 06:31

3 Answers3

3

There is no API in iOS to get this. Use this and make sure to update when there are new devices: https://github.com/lmirosevic/GBDeviceInfo

I don't think this would be a problem for the AppStore, but maybe don't include the jailbreak pod.

Also, you could just copy the info out of: https://github.com/lmirosevic/GBDeviceInfo/blob/master/GBDeviceInfo/GBDeviceInfo_iOS.m -- it's just a list of info about the device -- nothing crazy

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
0

Resultant code of my question and @lou-franco's answer:

//method
    -(float) getRealSize {
        float scale = 1.f;
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
            scale = [[UIScreen mainScreen] scale];
        }
        float ppi = [GBDeviceInfo deviceInfo].displayInfo.pixelsPerInch;
        if (ppi == 0) {
            ppi = 160.f;
        }
        CGRect originalFrame = [[UIScreen mainScreen] bounds];
        float w = originalFrame.size.width;
        float h = originalFrame.size.height;
        return sqrtf(w*w + h*h) * scale / ppi;
    }

//usage:
float inches = [self getRealSize];
Danny Dulai
  • 1,657
  • 12
  • 14
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0

There is no API. All you can do is lookup which device you’re running on, and have a table of physical screen sizes in your app. You need to update for new devices all the time. You may find a library for this.

In addition: if you try to take the size of a pixel from a table, check whether your device is in zoom mode - for example a 7+ in zoom mode will have the same number of pixels as a 7, but with a larger screen. That also means you can’t rely on the screen size in pixels. So any method using the number of pixels to determine the physical screen size will break in zoom mode.

That said, for newer devices that your code doesn’t know, a guess based on number of pixels is the best you can do.

And if your app supports external screens, for anything displayed on another screen you can’t know anything about the screen size.

gnasher729
  • 51,477
  • 5
  • 75
  • 98