Detecting (new) iPad (3) dpi and retina

You must Login before you can answer or comment on any questions.

Hi All,

This is not a really question, is a share of my code changes to detect the new iPad with retina display on Titanium mobile (mobilesdk-2.0.0.v20120314154741).

I change this 2 functions on TiUtils.m:

+(int) dpi
{
    if ([TiUtils isIPad])
    {
        if ([TiUtils isRetinaDisplay])
        {
            return 264;
        }
        else
        {
            return 130;
        }
    }
    else
    {
        if ([TiUtils isRetinaDisplay])
        {
            return 320;
        }
        else
        {
            return 160;
        }
    }    
}
 
+(BOOL)isRetinaDisplay
{
    // since we call this alot, cache it
    static CGFloat scale = 0.0;
    if (scale == 0.0)
    {
        // NOTE: In order to detect the Retina display reliably on all iOS devices,
        // you need to check if the device is running iOS4+ and if the 
        // [UIScreen mainScreen].scale property is equal to 2.0. 
        // You CANNOT assume a device is running iOS4+ if the scale property exists,
        // as the iPad 3.2 also contains this property.
        // On an iPad running iOS3.2, scale will return 1.0 in 1x mode, and 2.0
        // in 2x mode -- even though we know that device does not contain a Retina display.
        // Apple changed this behavior in iOS4.2 for the iPad: it returns 1.0 in both
        // 1x and 2x modes. You can test this yourself in the simulator.
        // I test for the -displayLinkWithTarget:selector: method on the main screen
        // which exists in iOS4.x but not iOS3.2, and then check the screen's scale:
 
        if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && 
            ([UIScreen mainScreen].scale == 2.0)) {
            scale = 2.0;
            return YES;
        } else {
            scale = 1.0;
            return NO;
        }   
 
    }
    return scale > 1.0;
}

and this function on TiPlatformDisplayCaps.m:

- (id)density
{
    if ([TiUtils isRetinaDisplay])
    {
        return @"high";
    }
    return @"medium";
}

Now the Ti respond correctly density and dpi.

1 Answer

Great work, exactly what I was looking for.

Not sure if this is 1.8 vs 2.0 issue, but the +(int) dpi method in 1.8 SDK is in TiPlatformDisplayCaps.m, not TiUtils.m.

Your Answer

Think you can help? Login to answer this question!