Search code examples
iosobjective-ciphoneipad

iOS device real size in inches


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.


Solution

  • 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