5

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!

Community
  • 1
  • 1
Shai UI
  • 50,568
  • 73
  • 204
  • 309
  • You can check link here http://stackoverflow.com/questions/3177634/iphone-how-do-i-detect-the-iphone-version – Hiren Jul 17 '12 at 10:31

2 Answers2

11

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);
}
Massimo Cafaro
  • 25,429
  • 15
  • 79
  • 93
  • Plus 1 for actually answering the question asked. Sometimes, it's useful to know the actual device model. For example, in my app, the iPhone 4 processor is slow, so I need to adjust some of MY features. – Jeff Jun 22 '13 at 07:40
9

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.

jer
  • 20,094
  • 5
  • 45
  • 69
  • 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 Sep 11 '10 at 05:47
  • 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 Sep 11 '10 at 05:52
  • 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. – Shai UI Sep 11 '10 at 06:03
  • Or you can test for the feature you need, regardless of what device its present on. – jer Sep 11 '10 at 06:11
  • Slight correction to this code: you need check if an instance of UIScreen responds to the selector, not the Class itself. It should be: [[UIScreen mainScreen] respondsToSelector:@selector(scale)] – commanda Dec 15 '10 at 03:09
  • With the code above, you should use >= instead of ==, in case of some sort of 3x-retina device. – kirb Jan 09 '12 at 11:38