Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically detect iphone 4 [duplicate]

Possible Duplicate:
iPhone - How do I detect the iPhone version?

So I looked at this thread: iPhone - How do I detect the iPhone version?

But it doesn't answer how to look for iphone 4. Also, it seems like ugly code. Maybe there's an easier way?

I'm also looking specifically for detecting iphone 4 in code. Any ideas? Thanks!

like image 206
Shai UI Avatar asked Sep 11 '10 05:09

Shai UI


2 Answers

I do not want to discuss here if it's better to detect the hardware model or the capabilities; I believe that what is best just depends on the particular need one may have. Anyway, if you want to test for the hardware model, then you can use the following unix system call:

#include <sys/utsname.h>

int uname(struct utsname *name);

One of the members of the struct utsname is machine, a null terminated string that identifies the hardware (for info about the other members see man 3 uname); in the case of an iPhone 4 it will be "iPhone3,1", for 3GS "iPhone2,1", for 3G "iPhone1,2", for the iPod Touch 4th generation "iPod4,1", third generation "iPod3,1" and second generation "iPod2,1".

struct utsname platform;
int rc;

rc = uname(&platform);
if(rc == -1){
 /* handle error */
}
else{
 fprintf(stdout, "hardware platform: %s", platform.machine);
}
like image 169
Massimo Cafaro Avatar answered Sep 22 '22 21:09

Massimo Cafaro


Don't detect models, detect capabilities.

For instance, if you are downloading an image from a web service which provides @2x images as well as normal sized images, you know that right now, that only works on the iPhone4 and iPod Touch 4, but who's to say that won't work on the iAmazingMagicThingy tomorrow? Test for the capability.

To use the above example, the way to test for that would be:

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

I'm not going to go on and on about how to test for capabilities, but you really want to be doing it this way rather than testing for a specific model.

like image 26
jer Avatar answered Sep 21 '22 21:09

jer