确定iPhone的版本

我正在开发一个应用程序,只能安装在iPhone 4及更高版本。我也通过UIRequiredDeviceCapabilities和设备兼容性matrix 。我需要更多的解决scheme。

那么,首先你可以使用UIDevice类作为string接收模型:

 [[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是一个包含设备生成的string,例如:

  • iPhone1,1iPhone1,1 (第一个iPhone)
  • iPhone1,2iPhone 3G
  • iPhone2,1iPhone 3GS
  • iPhone3,1iPhone3,2iPhone 4
  • iPhone iPhone4,1iPhone 4S
  • iPhone5,1iPhone5,2iPhone 5

请注意,在iPhone 4平台的逗号前面的数字实际上是3 ,而不是4.使用这个string,你可以隔离这个数字,并检查它是否大于或等于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