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