11

Possible Duplicate:
Detect Retina Display

How can we know if a device has a retina display or not from objective C code?

Community
  • 1
  • 1
Abhinav
  • 37,684
  • 43
  • 191
  • 309

3 Answers3

56
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]
    && [[UIScreen mainScreen] scale] >= 2.0) {
    // Retina
} else {
    // Not Retina
}
Stanislav Yaglo
  • 3,185
  • 23
  • 19
5

You can check the scale property on [UIScreen mainScreen] if it is 2.0 you are running on retina, if it is 1.0 you are not. You can also get the scale from the current CoreGraphics Context.

GorillaPatch
  • 5,007
  • 1
  • 39
  • 56
  • 2
    And the application will crash if the user runs it on iOS < 4 – Stanislav Yaglo Apr 06 '11 at 15:33
  • 2
    Correct. This is why you want to check first if [[UIScreen mainScreen] respondsToSelector:@selector(scale)] is true. This is the general concept of how you would code to ensure backwards compatibility. – GorillaPatch Apr 06 '11 at 19:54
-1

I don't think you can determine that directly. You'll have to infer it from the model information you can get back from sysctlbyname (see the iOS man pages). For example:

sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);

will give you back a string like "iPhone3,1" which has a retina display, or "iPhone 2,1" which hasn't.

onnoweb
  • 3,038
  • 22
  • 29
  • This is a terrible idea—it's not future-proof at all, and doesn't even account for the fourth-gen iPod touch, which also has a Retina display. The `UIScreen` class's `scale` property, as described in the other answers, is the correct way to do this. – Noah Witherspoon Apr 06 '11 at 15:41
  • 2
    Good point. I stand corrected. – onnoweb Apr 06 '11 at 15:45