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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…