views:

276

answers:

2

Hey Guys,

So I looked at this thread: http://stackoverflow.com/questions/3177634/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!

+3  A: 

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 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.

jer
An example of why this is better: the functionality you would have served the iPhone 4 will now also appropriately go towards the new iPod touch as well.
Jorge Israel Peña
Because the way it's being done in that post is fragile and will always be a catchup game. If you know what features you need, look for those features, and ignore what device they're on -- consider any device with them capable of running that code. Consider this: All things that fly have wings, but not all wing'd things can fly. So does it make more sense to check if the thing has wings and that they are capable of supporting the animal in flight? Or to get a list of all the birds you know can fly and comparing against that? Eventually, you may find a new species and your code is out of date.
jer
you guys are right.. but i was looking at http://www.iphonedevsdk.com/forum/iphone-sdk-development/45032-more-ipad-landscape-launch-issues.html and they don't account for iphone 4. i guess i should rethink how to do that. maybe just go with sleep(x) heh.
Shnitzel
Or you can test for the feature you need, regardless of what device its present on.
jer
A: 

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