xcode 如何在 iOS 8.3 中检测设备是否为 iPad?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29608613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I detect if device is an iPad in iOS 8.3?
提问by Ben Leggiero
We updated our SDK to iOS 8.3, and all of a sudden, our iPad detection method doesn't work properly:
我们将我们的 SDK 更新到了 iOS 8.3,突然之间,我们的 iPad 检测方法无法正常工作:
+ (BOOL) isiPad
{
#ifdef UI_USER_INTERFACE_IDIOM
return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
#endif
return NO;
}
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()
?
该ifdef
块永远不会进入,因此return NO;
总是运行。如何在不使用的情况下检测设备是否为 iPad UI_USER_INTERFACE_IDIOM()
?
I'm using:
我正在使用:
- 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)
- Xcode 6.3 (6D570)
- iOS 8.2 (12D508) - 使用 iOS 8.3 编译器进行编译
- 部署:目标设备系列:iPhone/iPad
- Mac OS X:优胜美地 (10.10.3)
- Mac:MacBook Pro (MacBookPro11,3)
回答by Warren Burton
In 8.2
UserInterfaceIdiom()
is
在8.2
UserInterfaceIdiom()
是
#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)
In 8.3
UserInterfaceIdiom()
is
在8.3
UserInterfaceIdiom()
是
static inline UIUserInterfaceIdiom UI_USER_INTERFACE_IDIOM() {
return ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ?
[[UIDevice currentDevice] userInterfaceIdiom] :
UIUserInterfaceIdiomPhone);
}
So #ifdef UI_USER_INTERFACE_IDIOM
is always false in 8.3
所以#ifdef UI_USER_INTERFACE_IDIOM
总是假的8.3
Note that the header says
请注意,标题说
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.
UI_USER_INTERFACE_IDIOM() 函数用于部署到低于 3.2 的 iOS 版本时使用。如果您要部署的最早版本的 iPhone/iOS 是 3.2 或更高版本,您可以直接使用 -[UIDevice userInterfaceIdiom]。
So suggest you refactor to
所以建议你重构为
+ (BOOL) isiPad
{
static BOOL isIPad = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
isIPad = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
});
return isIPad;
}