Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect if device is an iPad in iOS 8.3?

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;
}

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()?


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)
like image 749
Ky. Avatar asked Apr 13 '15 15:04

Ky.


People also ask

How do I know if my iPad has iPadOS?

Also, to check which version of iPadOS your iPad is using, go to Settings, Software Update, and your iPad will display which version you are on.

Is my iPad considered iOS?

Apple's iPhone runs the iOS operating system, while iPad run iPadOS—based on iOS.


1 Answers

In 8.2 UserInterfaceIdiom() is

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

In 8.3 UserInterfaceIdiom() is

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

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.

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;
}
like image 178
Warren Burton Avatar answered Sep 28 '22 01:09

Warren Burton