¿Cuál es el código para detectar si la aplicación ios se está ejecutando en un iPhone, una pantalla Retina de iPhone o un iPad?

¿Cuál es el código para detectar si la aplicación ios se está ejecutando en un iPhone, una pantalla Retina de iPhone o un iPad?

Antecedentes:

  • 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.

preguntado el 27 de agosto de 11 a las 20:08

5 Respuestas

Puedes usar [[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 use imageNamed and append "@2x" to your high resolution image file names (see Compatible con pantallas de alta resolución in the Drawing and Printing Guide for iOS).

If you really need to know which resolution the screen has, use UIScreen's scale método.

Respondido 28 ago 11, 01:08

nota [[UIDevice currentDevice] userInterfaceIdiom] can be checked against UIUserInterfaceIdiomPhone or UIUserInterfaceIdiomPad - no polaridad

Here's 2 useful class methods that I use, which directly answers your question - which you may want to use further down the line:

+(BOOL)isPad
{
#ifdef UI_USER_INTERFACE_IDIOM
    return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
#endif
    return NO;
}

+(BOOL)hasRetinaDisplay
{
    // checks for iPhone 4. will return a false positive on iPads, so use the above function in conjunction with this to determine if it's a 3GS or below, or an iPhone 4.
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2)
        return YES;
    else
        return NO;
}

Respondido 29 ago 11, 00:08

For Swift Solution:

 if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad)     
 {
        // Ipad
 }
 else 
 {
       // Iphone
 }

respondido 16 mar '15, 17:03

ver @interface UIDevice

as well as the documentation at -[UIImage scale] (although there are better resources, which will likely be posted).

Respondido 28 ago 11, 00:08

Here is some code to copy and paste...

bool runningOniPhone;
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    runningOniPhone = TRUE;
} else {
    runningOniPhone = FALSE;
}

Respondido 25 Oct 13, 05:10

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.