iPad Pro设备检测

时间:2015-10-31 15:34:00

标签: ios objective-c ipad

我正在尝试检测iPad Pro设备,尝试用以下方法猜测它的高度:

NSLog(@"%f",self.view.frame.size.height);

但它会返回1024!与iPad非视网膜设备相同。有什么建议吗?

我需要使用以下代码行为iPad Pro指定一些代码:

#define iPadPro ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && [UIScreen mainScreen].bounds.size.height == 2732)

...即使在iOS模拟器上,代码也必须检测iPad Pro!

由于

已编辑: 有人建议使用LunchScreen,但是当我使用它时,会发生这种情况(按比例缩小): enter image description here

11 个答案:

答案 0 :(得分:14)

特别感谢@rmaddy

检测屏幕尺寸的正确方法是:

NSLog(@"%f",[UIScreen mainScreen].bounds.size.height);

现在,如果您的应用程序以Portrait模式运行,您只需使用此代码即可检测iPad Pro:

#define iPadPro ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && [UIScreen mainScreen].bounds.size.height == 1366)
  

不要忘记使用LaunchScreen的需要,否则应用程序将无法利用iPad Pro的大屏幕

答案 1 :(得分:8)

无论设备的方向如何,

检测iPad pro 12.9英寸

#define iPadPro12 (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad && UIScreen.mainScreen.nativeBounds.size.height == 2732) 

答案 2 :(得分:5)

这是一个无论设备方向如何都可以使用的检查:

- (BOOL)isIpadPro
{
    UIScreen *mainScreen = [UIScreen mainScreen];
    CGFloat width = mainScreen.nativeBounds.size.width / mainScreen.nativeScale;
    CGFloat height = mainScreen.nativeBounds.size.height / mainScreen.nativeScale;
    BOOL isIpad = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
    BOOL hasIPadProWidth = fabs(width - 1024.f) < DBL_EPSILON;
    BOOL hasIPadProHeight = fabs(height - 1366.f) < DBL_EPSILON;
    return isIpad && hasIPadProHeight && hasIPadProWidth;
}

请注意,这仅适用于iOS8 +

答案 3 :(得分:5)

感谢@ Mc.Lover

PortraitLandscape方向的一点点更新:

if (([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && ([UIScreen mainScreen].bounds.size.height == 1366 || [UIScreen mainScreen].bounds.size.width == 1366))) {
//iPad Pro
}

答案 4 :(得分:5)

Swift 3 +的固定解决方案

有两种解决方案,第一种是我在项目中使用过的,你可以使用其中任何一种。

1-使用宏

2-使用扩展

使用宏

#define iPadPro129 (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad && 
                                  UIScreen.mainScreen.nativeBounds.size.height == 2732) 

#define iPadPro105 (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad && 
                                  UIScreen.mainScreen.nativeBounds.size.height == 2224) 

使用扩展

extension UIDevice {

    // for ipad pro 12.9 device
     public var isPadPro129: Bool {
         if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad 
             && UIScreen.main.nativeBounds.size.height == 2732) {
              return true
         }
         return false
     }



  // for ipad pro 10.5 device
     public var isPadPro105: Bool {
         if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad 
             && UIScreen.main. nativeBounds.size.height == 2224) {
              return true
         }
         return false
     }

}

答案 5 :(得分:4)

D1mers0n解决方案的Swift版本。

func isIpadPro() -> Bool
{
    if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad &&
        (UIScreen.mainScreen().bounds.size.height == 1366 || UIScreen.mainScreen().bounds.size.width == 1366)) {
        return true
    }
    return false
}

答案 6 :(得分:4)

遵循Swift3标准/实践,更好的方法是:

extension UIDevice {

     public var isiPadPro12: Bool {
         if UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad 
             && (UIScreen.main.bounds.size.height == 1366 || UIScreen.main.bounds.size.width == 1366) {
              return true
         }
         return false
     }

}

将其添加到extension UIDevice可让您拨打电话,这很整洁:

UIDevice.current.isiPadPro12

有一天,Apple最终会给我们一个enum价值!

答案 7 :(得分:1)

这不是最好的长期解决方案,但它今天对你有用......

获取设备的硬件字符串......

size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);

NSString *hardwareString = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);

将硬件字符串与这些hw字符串进行比较:

if ([hardwareString isEqualToString:@"iPad6,7"] || [hardwareString isEqualToString:@"iPad6,8"]) {
    // iPad Pro
}

如果你愿意,我有一个课程,包括我可以寄给你的所有这些。从来没有真正打磨它足以制作一个吊舱,但如果需要,我确实有它。

答案 8 :(得分:0)

Swift 3版Justin Domnitz对D1mers0n解决方案的解决方案:

func isIpadPro12() -> Bool
    {
        if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad 
        && (UIScreen.main.bounds.size.height == 1366 || UIScreen.main.bounds.size.width == 1366)) {
            return true
        }
        return false
    }

答案 9 :(得分:0)

这就是我使用的

extension UIDevice {

    public class var isiPad: Bool {
        return UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
    }

    public class var isiPadPro129: Bool {
        return isiPad && UIScreen.main.nativeBounds.size.height == 2732
    }

    public class var isiPadPro97: Bool {
        return isiPad && UIScreen.main.nativeBounds.size.height == 2048
    }

    public class var isiPadPro: Bool {
        return isiPad && (isiPadPro97 || isiPadPro129)
    }

}

答案 10 :(得分:0)

我在Objective-C中使用宏的解决方案:

define IS_IPAD_DEVICE   ([(NSString *)[UIDevice currentDevice].model hasPrefix:@"iPad"])    
define IS_IPAD_IDIOM    (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)    
define IS_IPAD          (IS_IPAD_DEVICE || IS_IPAD_IDIOM)    
define IS_PORTRAIT                              UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])    
define IS_LANDSCAPE                             UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])    
define IS_IPAD_PRO_12_9  (IS_IPAD && ((IS_PORTRAIT && [[UIScreen mainScreen] nativeBounds].size.height == 2732.0) || (IS_LANDSCAPE && [[UIScreen mainScreen] nativeBounds].size.height == 2048.0)))    
define IS_IPAD_PRO_11    (IS_IPAD && ((IS_PORTRAIT && [[UIScreen mainScreen] nativeBounds].size.height == 2388.0) || (IS_LANDSCAPE && [[UIScreen mainScreen] nativeBounds].size.height == 1668.0)))    
define IS_IPAD_PRO_10_5  (IS_IPAD && ((IS_PORTRAIT && [[UIScreen mainScreen] nativeBounds].size.height == 2224.0) || (IS_LANDSCAPE && [[UIScreen mainScreen] nativeBounds].size.height == 1668.0)))    
define IS_IPAD_OR_MINI   (IS_IPAD && ((IS_PORTRAIT && [[UIScreen mainScreen] nativeBounds].size.height == 2048.0) || (IS_LANDSCAPE && [[UIScreen mainScreen] nativeBounds].size.height == 1536.0)))