开发者

Determine iPhone device at runtime

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜