views:

166

answers:

2

Is ther any way to perform a runtime check for the iPhone device at runtime? It has to be able to differenciate iPhone 4 from other iPhone/iPod touch models. Any workaround that does the same thing is OK too.

+2  A: 

I use some code like this for the same purpose:

if([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    return [[UIScreen mainScreen] scale] == 2.0 ? YES : NO;
return NO;

Only iOS 4.x+ devices support the UIScreen scale instance method. And since iPhone4's don't run iOS 3, we can rule those out right away. Next, we check if the scale factor is 2.0, if so we know it has a retina display.

While this isn't definitive (apple could release another retina device tomorrow), it does test 'model' where it's important -- i.e., you could be fetching images from a web service that provides @2x images and standard images, which is what I'm doing, which means you need to write the scaling image support manually, you don't get it for free as with UIImage's -imageNamed: for local files.

jer
Thanks! That was exactly what I needed.
tadej5553
A: 

You can get the exact model through the UIDevice class:

[[UIDevice currentDevice] model]

This, and some other methods are documented here: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html

Be careful, though, to not confuse hardware model with iOS version. If you want to provide extra/different functionality for devices that support them, it's better to check if that specific interface is available, using the respondsToSelector: method, or the NSClassFromString function.

Be careful with using the NSClassFromString function, though, since some classes exists as a part of the private API in the earlier SDK:s, with a completely different interface.

geon
I looked through that, and it only differenciates between iPod and iPhone, not between different versions of iPhone.
tadej5553