what is the code to detect whether ios app running in iPhone, iPhone Retina display, or iPad?

前端 未结 5 1642
走了就别回头了
走了就别回头了 2021-01-02 06:03

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 i

相关标签:
5条回答
  • 2021-01-02 06:12

    see @interface UIDevice

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

    0 讨论(0)
  • 2021-01-02 06:24

    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;
    }
    
    0 讨论(0)
  • 2021-01-02 06:28

    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 use imageNamed 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's scale method.

    0 讨论(0)
  • 2021-01-02 06:28

    For Swift Solution:

     if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad)     
     {
            // Ipad
     }
     else 
     {
           // Iphone
     }
    
    0 讨论(0)
  • 2021-01-02 06:29

    Here is some code to copy and paste...

    bool runningOniPhone;
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        runningOniPhone = TRUE;
    } else {
        runningOniPhone = FALSE;
    }
    
    0 讨论(0)
提交回复
热议问题