Is it possible to detect that your iOS app is running on an iPad mini at runtime?

Boxel’s answer would be good if it didn’t invoke undefined behavior and if it didn’t have superfluous parts. One, + [NSString stringWithCString:encoding:] requires a C string – that is, a NUL-terminated char pointer (else it will most likely dump core). Also, you don’t need the conversion to NSString – since sysctlbyname() provides you with a plain old C string (without the NUL terminator, of course), you can directly use strcmp() to save a few dozen CPU cycles:

#include <sys/sysctl.h>

size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size + 1);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
machine[size] = 0;

if (strcmp(machine, "iPad2,5") == 0) {
    /* iPad mini */
}

free(machine);

Edit: now that answer is fixed as well.

Leave a Comment