8

I need to distinguish iPhone 4S from iPhone 4. I went through the posts and got the following code. Please let me know if this is the only way of doing it and confirm that there is no problem by Apple if used.

            struct utsname systemInfo;
            uname(&systemInfo);
            NSString *iPhoneVersion = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

For 4S, iPhoneVersion string will give me iPhone4,1 and iPhone 4 will say iPhone3,1.

Thanks for your suggestions.

Proud Member
  • 40,078
  • 47
  • 146
  • 231
Sana
  • 144
  • 7
  • possible duplicate of [How to tell if your code is running on an iPhone or an iPhone3G?](http://stackoverflow.com/questions/688001/how-to-tell-if-your-code-is-running-on-an-iphone-or-an-iphone3g) – 一二三 Nov 07 '11 at 13:11

2 Answers2

6

iPhone 4 may also be called iPhone3,3 in the case of the CDMA model (here is a not-quite-up-to-date list). In general it is much safer and "forward compatible" to test for the capabilities of the platform (e.g. [someSystemClass respondsToSelector:@selector(someMethodName)] or similar) rather than manually determining the capabilities based on the device model. Could you say why you need to distinguish between iPhone 4 and 4S?

Martin Gjaldbaek
  • 2,987
  • 4
  • 20
  • 29
  • One reason I could think the OP would want is reporting usage stats per device. – jbrennan Nov 07 '11 at 17:45
  • 2
    If you report usage stats then it is much better to simply use the raw value of the hardware type and then sort it out on the server that it is reporting to. Then you don't have to update your app every time Apple introduces new hardware. – Stefan Arentz Nov 07 '11 at 17:49
  • @Martin: I want to display the photo resolutions, the original one and a medium one for the iPhone devices. Using [[UIScreen mainScreen] scale] method, I could separate out 3GS from iPhone 4. – Sana Nov 08 '11 at 05:01
  • 1
    Ok, unfortunately I don't know a good way to do that "the right way". See discussion here: http://stackoverflow.com/questions/1638161/get-iphones-camera-resolution – Martin Gjaldbaek Nov 08 '11 at 15:32
5

As others have suggested, rather than selecting for a specific device (which will break if and when Apple rolls out variations or the next model), you should focus on what capabilities you need. Just about the only iPhone applications that broke when the iPad launched were ones that did hardware model detection, because they had no idea that the iPad was coming and thus couldn't look for that specific device ID.

If your application absolutely cannot run on an iPhone 4, due to specific hardware requirements like Bluetooth 4.0 Low Power support, you'd be best served to add the bluetooth-le to your UIRequiredDeviceCapabilities in your Info.plist, like I describe here. That will prevent users from even installing your application on a non-4S device, but leaves open the possibility of this working for future iPhones which will most likely have Bluetooth Low Power hardware.

If you need to scale hardware capabilities to take advantage of the A5, you might want to look into checking for support for some of the new OpenGL ES extensions that the A5 has, by using glGetString( GL_EXTENSIONS ) and checking for extensions like APPLE_shadow_samplers. These extensions aren't present on the A4, but are there on the A5 and should be there for the next couple of iterations of Apple's hardware.

You might also be able to use the lightly documented new Core Bluetooth framework to test for the presence of 4.0 Low Power hardware in your device at runtime, but I haven't spent much time with that framework.

Community
  • 1
  • 1
Brad Larson
  • 170,088
  • 45
  • 397
  • 571