Programmatically detect whether an iPad has Retina display?

前端 未结 2 1232
情深已故
情深已故 2020-12-10 03:31

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

相关标签:
2条回答
  • 2020-12-10 04:21
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && [[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [UIScreen mainScreen].scale > 1)
    {
        // new iPad
    }
    
    0 讨论(0)
  • 2020-12-10 04:23

    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");
    
    0 讨论(0)
提交回复
热议问题