且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以在运行时检测到您的 iOS 应用程序正在 iPad mini 上运行?

更新时间:2023-10-16 11:44:16

如果 Boxel 没有调用未定义的行为并且没有多余的部分,它的答案会很好.一, + [NSString stringWithCString:encoding:] 需要一个 C 字符串 - 即一个 NUL 终止的字符指针(否则它很可能会转储核心).此外,您不需要转换为 NSString - 因为 sysctlbyname() 为您提供了一个普通的旧 C 字符串(当然没有 NUL 终止符),您可以直接用strcmp()节省几十个CPU周期:

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);

现在该答案也已修复.