Programmatically detect whether an iPad has Retina display?

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

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

2条回答
  •  甜味超标
    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 
    #include 
    
    // 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");
    

提交回复
热议问题