如何以编程方式检测iphone 4

for*_*yez 5 iphone objective-c

可能重复:
iPhone - 如何检测iPhone版本?

所以我看了这个帖子: iPhone - 我如何检测iPhone版本?

但它没有回答如何寻找iphone 4.此外,它似乎是丑陋的代码.也许有一种更简单的方法?

我也正在寻找专门用于检测代码中的iphone 4的方法.有任何想法吗?谢谢!

Mas*_*aro 11

我不想在这里讨论是否更好地检测硬件模型或功能; 我相信最好的只取决于他们可能拥有的特殊需求.无论如何,如果你想测试硬件模型,那么你可以使用以下unix系统调用:

#include <sys/utsname.h>

int uname(struct utsname *name);
Run Code Online (Sandbox Code Playgroud)

其中一个成员struct utsnamemachine一个空终止字符串,用于标识硬件(有关其他成员的信息,请参阅man 3 uname); 对于iPhone 4,它将是"iPhone3,1",用于3GS"iPhone2,1",用于3G"iPhone1,2",用于iPod Touch第4代"iPod4,1",第三代"iPod3,1" "和第二代"iPod2,1".

struct utsname platform;
int rc;

rc = uname(&platform);
if(rc == -1){
 /* handle error */
}
else{
 fprintf(stdout, "hardware platform: %s", platform.machine);
}
Run Code Online (Sandbox Code Playgroud)


jer*_*jer 9

不检测模型,检测功能.

例如,如果您从提供@ 2x图像以及正常尺寸图像的Web服务下载图像,您现在就知道,这仅适用于iPhone4和iPod Touch 4,但是谁说不会明天在iAmazingMagicThingy上工作?测试能力.

要使用上面的示例,测试的方法是:

if([[UIScreen mainScreen] respondsToSelector:@selector(scale)] &&
   [[UIScreen mainScreen] scale] == 2.0) {
    /* do your thing */
}
Run Code Online (Sandbox Code Playgroud)

我不打算继续讨论如何测试功能,但是你真的希望这样做而不是测试特定的模型.