Reputation: 6051
I am trying to detect iPad Pro device , trying to guess its height with :
NSLog(@"%f",self.view.frame.size.height);
But it returns 1024
! same as iPad non retina devices . any advice ?
I need to specify some codes exact for iPad Pro with this line of code :
#define iPadPro ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && [UIScreen mainScreen].bounds.size.height == 2732)
...and the codes must detect iPad Pro even on iOS simulator !
Thanks
EDITED : Some suggest use LunchScreen , but when I use it this happens (scaled down) :
Upvotes: 21
Views: 16657
Reputation: 21
My solution in Objective-C using macros:
define IS_IPAD_DEVICE ([(NSString *)[UIDevice currentDevice].model hasPrefix:@"iPad"])
define IS_IPAD_IDIOM (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
define IS_IPAD (IS_IPAD_DEVICE || IS_IPAD_IDIOM)
define IS_PORTRAIT UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])
define IS_LANDSCAPE UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])
define IS_IPAD_PRO_12_9 (IS_IPAD && ((IS_PORTRAIT && [[UIScreen mainScreen] nativeBounds].size.height == 2732.0) || (IS_LANDSCAPE && [[UIScreen mainScreen] nativeBounds].size.height == 2048.0)))
define IS_IPAD_PRO_11 (IS_IPAD && ((IS_PORTRAIT && [[UIScreen mainScreen] nativeBounds].size.height == 2388.0) || (IS_LANDSCAPE && [[UIScreen mainScreen] nativeBounds].size.height == 1668.0)))
define IS_IPAD_PRO_10_5 (IS_IPAD && ((IS_PORTRAIT && [[UIScreen mainScreen] nativeBounds].size.height == 2224.0) || (IS_LANDSCAPE && [[UIScreen mainScreen] nativeBounds].size.height == 1668.0)))
define IS_IPAD_OR_MINI (IS_IPAD && ((IS_PORTRAIT && [[UIScreen mainScreen] nativeBounds].size.height == 2048.0) || (IS_LANDSCAPE && [[UIScreen mainScreen] nativeBounds].size.height == 1536.0)))
Upvotes: 2
Reputation: 2650
There are two solutions , First one I've used in my project , you can use any one of them.
1- Using Macro
2- Using Extension
#define iPadPro129 (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad &&
UIScreen.mainScreen.nativeBounds.size.height == 2732)
#define iPadPro105 (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad &&
UIScreen.mainScreen.nativeBounds.size.height == 2224)
extension UIDevice {
// for ipad pro 12.9 device
public var isPadPro129: Bool {
if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
&& UIScreen.main.nativeBounds.size.height == 2732) {
return true
}
return false
}
// for ipad pro 10.5 device
public var isPadPro105: Bool {
if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
&& UIScreen.main. nativeBounds.size.height == 2224) {
return true
}
return false
}
}
Upvotes: 5
Reputation: 10329
This is what I use, for swift3:
extension UIDevice {
public class var isiPad: Bool {
return UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
}
public class var isiPadPro129: Bool {
return isiPad && UIScreen.main.nativeBounds.size.height == 2732
}
public class var isiPadPro97: Bool {
return isiPad && UIScreen.main.nativeBounds.size.height == 2048
}
public class var isiPadPro: Bool {
return isiPad && (isiPadPro97 || isiPadPro129)
}
}
Upvotes: 0
Reputation: 1529
Following Swift3 standards/practices, a better approach would be:
extension UIDevice {
public var isiPadPro12: Bool {
if UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
&& (UIScreen.main.bounds.size.height == 1366 || UIScreen.main.bounds.size.width == 1366) {
return true
}
return false
}
}
Adding this to an extension
of UIDevice
allows you to call, which is tidy:
UIDevice.current.isiPadPro12
One day Apple will finally give us an enum
value!
Upvotes: 4
Reputation: 48
Swift 3 Version of Justin Domnitz's solution of D1mers0n's solution:
func isIpadPro12() -> Bool
{
if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
&& (UIScreen.main.bounds.size.height == 1366 || UIScreen.main.bounds.size.width == 1366)) {
return true
}
return false
}
Upvotes: 0
Reputation: 568
detect iPad pro 12.9 inch no matter device's orientation
#define iPadPro12 (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad && UIScreen.mainScreen.nativeBounds.size.height == 2732)
Upvotes: 9
Reputation: 3307
Swift version of D1mers0n's solution.
func isIpadPro() -> Bool
{
if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad &&
(UIScreen.mainScreen().bounds.size.height == 1366 || UIScreen.mainScreen().bounds.size.width == 1366)) {
return true
}
return false
}
Upvotes: 4
Reputation: 14251
Here is a check that would work regardless of the device orientation:
- (BOOL)isIpadPro
{
UIScreen *mainScreen = [UIScreen mainScreen];
CGFloat width = mainScreen.nativeBounds.size.width / mainScreen.nativeScale;
CGFloat height = mainScreen.nativeBounds.size.height / mainScreen.nativeScale;
BOOL isIpad = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
BOOL hasIPadProWidth = fabs(width - 1024.f) < DBL_EPSILON;
BOOL hasIPadProHeight = fabs(height - 1366.f) < DBL_EPSILON;
return isIpad && hasIPadProHeight && hasIPadProWidth;
}
Please note that this only works for iOS8+
Upvotes: 5
Reputation: 470
Thanks to @Mc.Lover
A little update for both Portrait
and Landscape
orientations:
if (([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && ([UIScreen mainScreen].bounds.size.height == 1366 || [UIScreen mainScreen].bounds.size.width == 1366))) {
//iPad Pro
}
Upvotes: 5
Reputation: 6051
Special thanks to @rmaddy
The proper way to detect screens sizes is :
NSLog(@"%f",[UIScreen mainScreen].bounds.size.height);
Now if your application runs in Portrait
mode you can simply use this code to detect iPad Pro :
#define iPadPro ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && [UIScreen mainScreen].bounds.size.height == 1366)
Don't forget the need to use a LaunchScreen or the app won't take advantage of the iPad Pro's larger screen
Upvotes: 15
Reputation: 4677
This isn't the best long term solution, but it will work for you today...
Get the hardware string for the device...
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *hardwareString = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);
Compare the hardware string to these hw strings:
if ([hardwareString isEqualToString:@"iPad6,7"] || [hardwareString isEqualToString:@"iPad6,8"]) {
// iPad Pro
}
If you like, I have a class that wraps all of this that I could send you. Never really polished it enough to make a pod out of it, but I do have it available if need be.
Upvotes: 1