what is the code to detect whether ios app running in iPhone, iPhone Retina display, or iPad?
Background:
for my iPhone application I have defined in XCode target/summary page the specific images for: iPhone launch image, iPhone retina display launch image, iPad portrait & iPad landscape.
in the main view there is a UIImageView subview I use for the background image - currently I'm specifying this in XCode (not programmatically) by selecting the image I use for the iPhone launch image.
So I'm asking how to tell which one I'm running within so that in the viewDidLoad I can load the appropriate resolution background image. Then there should be a seamless transition between the background image for app startup, and the background of the app main screen once it's started.
You can use
[[UIDevice currentDevice] userInterfaceIdiom]
to determine whether you're running on an iPhone/iPod touch or an iPad.There's often no need to determine directly whether you're on a retina display because
UIImage
handles that automatically when you useimageNamed
and append "@2x" to your high resolution image file names (see Supporting High-Resolution Screens in the Drawing and Printing Guide for iOS).If you really need to know which resolution the screen has, use
UIScreen
'sscale
method.see
@interface UIDevice
as well as the documentation at
-[UIImage scale]
(although there are better resources, which will likely be posted).For Swift Solution:
Here is some code to copy and paste...
Here's 2 useful class methods that I use, which directly answers your question - which you may want to use further down the line: