Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically detect whether an iPad has Retina display?

How can I programmatically (Objective-C) whether an iPad has a Retina display?

like image 296
António Avatar asked Mar 13 '12 17:03

António


2 Answers

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && [[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [UIScreen mainScreen].scale > 1)
{
    // new iPad
}
like image 118
Noah Witherspoon Avatar answered Sep 22 '22 05:09

Noah Witherspoon


As other posters have answered, you should check for features rather than models. However, in the few obscure cases where you might want to identify a particular model, you can use the hw.machine sysctrl as follows. Note that if you can't identify the model, it's most likely because your code is running on a new model, so you should do something sensible in that case.

#include <sys/types.h>
#include <sys/sysctl.h>

// Determine the machine name, e.g. "iPhone1,1".
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0); // Get size of data to be returned.
char *name = malloc(size);
sysctlbyname("hw.machine", name, &size, NULL, 0);

NSString *machine = [NSString stringWithCString:name encoding:NSASCIIStringEncoding];
free(name);

Now you can compare "machine" against known values. E.g., to detect iPad (March 2012) models:

if ([machine hasPrefix:@"iPad3,"]) NSLog(@"iPad (March 2012) detected");
like image 30
bleater Avatar answered Sep 19 '22 05:09

bleater