How to determine if an iPad is an iPad in iOS 8.3?

We upgraded our SDK to iOS 8.3, and suddenly our iPad discovery method does not work as expected:

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

the ifdef block ifdef never entered, so return NO; always starts. How to determine if an iPad is using UI_USER_INTERFACE_IDIOM() ?


I use:

  • Xcode 6.3 (6D570)
  • iOS 8.2 (12D508) - Compiling with the iOS 8.3 Compiler
  • Deployment: Target Device Family: iPhone / iPad
  • Mac OS X: Yosemite (10.10.3)
  • Mac: MacBook Pro (MacBookPro11.3)
+6
source share
1 answer

In 8.2 UserInterfaceIdiom() has

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

In 8.3 UserInterfaceIdiom() has

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

So #ifdef UI_USER_INTERFACE_IDIOM always false at 8.3

Note that the title says

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

We offer you refactoring

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

Source: https://habr.com/ru/post/985071/


All Articles