首先,您可以使用UIDevice 类将模型作为字符串接收:
[[UIDevice currentDevice] model]
这将返回“iPod touch”或“iPhone”之类的内容。
您可以使用以下代码获得确切的模型平台:
(你必须#include <sys/types.h>和<sys/sysctl.h>)
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = @(machine); // Old syntax: [NSString stringWithCString:machine encoding:NSUTF8StringEncoding]
free(machine);
现在platform 是一个包含设备生成的字符串,例如:
-
iPhone1,1 iPhone 2G(第一部 iPhone)
-
iPhone1,2 iPhone 3G
-
iPhone2,1 iPhone 3GS
-
iPhone3,1 或 iPhone3,2 适用于 iPhone 4
-
iPhone4,1 iPhone 4S
-
iPhone 5 的
iPhone5,1 或 iPhone5,2
注意,iPhone 4平台逗号前面的数字实际上是3,而不是4。
使用此字符串,您可以隔离此数字并检查它是否大于或等于 3:
if ([[[UIDevice currentDevice] model] isEqualToString:@"iPhone"]) {
if ([[[platform componentsSeparatedByString:@","][0] substringFromIndex:6] intValue] >= 3) {
// Device is iPhone 4 or newer
} else {
// Device is older than iPhone 4
}
}
但是:您实际上可以检查屏幕的比例,因为 iPhone 4 是第一款配备视网膜显示屏的 iPhone:
[[UIScreen mainScreen] scale] // Returns 2.0f on the iPhone 4 and newer and 1.0f on older devices