如何在iOS 8.3中检测设备是否为iPad? [英] How can I detect if device is an iPad in iOS 8.3?

查看:115
本文介绍了如何在iOS 8.3中检测设备是否为iPad?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将SDK更新到iOS 8.3,突然之间,我们的iPad检测方法无法正常运行:

We updated our SDK to iOS 8.3, and all of a sudden, our iPad detection method doesn't work properly:

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

ifdef 块永远不会输入,所以返回NO; 始终运行。 如何在不使用 UI_USER_INTERFACE_IDIOM()的情况下检测设备是否为iPad?

the ifdef block is never entered, and so return NO; is always run. How do I detect if the device is an iPad without using UI_USER_INTERFACE_IDIOM()?

我正在使用:


  • Xcode 6.3(6D570)

  • iOS 8.2(12D508) - 使用iOS 8.3编译器进行编译

  • 部署:目标设备系列:iPhone / iPad

  • Mac OS X :约塞米蒂(10.10.3)

  • Mac:MacBook Pro(MacBookPro11,3)

  • Xcode 6.3 (6D570)
  • iOS 8.2 (12D508) - Compiling with iOS 8.3 compiler
  • Deployment: Targeted Device Family: iPhone/iPad
  • Mac OS X: Yosemite (10.10.3)
  • Mac: MacBook Pro (MacBookPro11,3)

推荐答案

8.2 UserInterfaceIdiom()

#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)

8.3 UserInterfaceIdiom()

static inline UIUserInterfaceIdiom UI_USER_INTERFACE_IDIOM() {
    return ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ?
            [[UIDevice currentDevice] userInterfaceIdiom] :
            UIUserInterfaceIdiomPhone);
}

所以 #ifdef UI_USER_INTERFACE_IDIOM 8.3总是假的

请注意标题是


提供UI_USER_INTERFACE_IDIOM()函数,以便在
部署到小于3.2的iOS版本时使用。如果您要部署的最早的
版本的iPhone / iOS是3.2或
更大,您可以直接使用 - [UIDevice userInterfaceIdiom]。

The UI_USER_INTERFACE_IDIOM() function is provided for use when deploying to a version of the iOS less than 3.2. If the earliest version of iPhone/iOS that you will be deploying for is 3.2 or greater, you may use -[UIDevice userInterfaceIdiom] directly.

所以建议你重构

+ (BOOL) isiPad
{
    static BOOL isIPad = NO;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        isIPad = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
    });
    return isIPad;
}

这篇关于如何在iOS 8.3中检测设备是否为iPad?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆