如何在iOS 8.3中检测设备是否为iPad?
我们将SDK更新为iOS 8.3,突然,我们的iPad检测方法无法正常工作:如何在iOS 8.3中检测设备是否为iPad?,ios,xcode,ipad,ios8.3,Ios,Xcode,Ipad,Ios8.3,我们将SDK更新为iOS 8.3,突然,我们的iPad检测方法无法正常工作: + (BOOL) isiPad { #ifdef UI_USER_INTERFACE_IDIOM return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad; #endif return NO; } 从不输入ifdef块,因此返回NO始终运行如果不使用UI\u USER\u INTERFACE\u IDIOM(),如何检测设备是否为iPad?
+ (BOOL) isiPad
{
#ifdef UI_USER_INTERFACE_IDIOM
return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
#endif
return NO;
}
从不输入ifdef
块,因此返回NO代码>始终运行如果不使用UI\u USER\u INTERFACE\u IDIOM()
,如何检测设备是否为iPad?
我正在使用:
- Xcode 6.3(6D570)
- iOS 8.2(12D508)-使用iOS 8.3编译器编译
- 部署:目标设备系列:iPhone/iPad
- MacOSX:Yosemite(10.10.3)
- Mac:MacBookPro(MacBookPro11,3)
8.2中的是
#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)
static inline UIUserInterfaceIdiom UI_USER_INTERFACE_IDIOM() {
return ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ?
[[UIDevice currentDevice] userInterfaceIdiom] :
UIUserInterfaceIdiomPhone);
}
在8.3
UserInterfaceIdiom()
中
#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)
static inline UIUserInterfaceIdiom UI_USER_INTERFACE_IDIOM() {
return ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ?
[[UIDevice currentDevice] userInterfaceIdiom] :
UIUserInterfaceIdiomPhone);
}
因此,在8.3
注意,标题是
提供UI\u USER\u INTERFACE\u IDIOM()函数,以便在以下情况下使用:
部署到低于3.2的iOS版本。如果最早
您将部署的iPhone/iOS版本为3.2或更高版本
更大,您可以直接使用-[UIDevice userInterfaceIdiom]
因此,建议您重构以
+ (BOOL) isiPad
{
static BOOL isIPad = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
isIPad = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
});
return isIPad;
}
右键单击UI\u USER\u INTERFACE\u IDIOM
,然后选择查找定义。它显示了什么?@trojanfoe看起来不错。UIKit
文档显示它仍然有效。我想您需要显示更多的代码(即使用该方法的代码)。@特洛伊木马在我们的代码中使用了数千次此isiPad
方法。每一次,据我所知,行为就像我上面描述的那样;跳转到返回NO代码>。为什么需要检查#ifdef
UI\u USER\u INTERFACE\u IDIOM
在所有受支持的iOS版本中都可用。这4行代码只运行一次,是吗?是的,这就是dispatch\u once函数实现的功能:-)好的。使用类似于#define(定义)isiPad[[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad
,然后只返回(定义)这样的值也行吗?不需要只计算一次这个值。只做一次比较便宜,因为iPad一般不会在运行的中途变成iPhone。因此,如果符合您的目的,可以定义。引用的文件告诉你确切的真相。Cmd单击Xcode中的UI\u USER\u INTERFACE\u IDIOM()
,如果您想自己阅读,可以转到页眉。因为大概是为了使用Swift实现这一功能,它变成了一个内联函数。对于使用#ifdef
进行的查询“它是否定义为C预处理器宏”,答案是否定的。