iPad 专业设备检测
iPad Pro device detection
我正在尝试检测 iPad Pro 设备,尝试通过以下方式猜测其高度:
NSLog(@"%f",self.view.frame.size.height);
但是returns1024
!与 iPad 非视网膜设备相同。有什么建议吗?
我需要用这行代码为 iPad Pro 指定一些代码:
#define iPadPro ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && [UIScreen mainScreen].bounds.size.height == 2732)
...并且即使在 iOS 模拟器上,代码也必须检测到 iPad Pro!
谢谢
已编辑:
有些人建议使用 LunchScreen ,但是当我使用它时会发生这种情况(按比例缩小):
这不是最好的长期解决方案,但今天对您有用...
获取设备的硬件字符串...
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);
将硬件字符串与这些硬件字符串进行比较:
if ([hardwareString isEqualToString:@"iPad6,7"] || [hardwareString isEqualToString:@"iPad6,8"]) {
// iPad Pro
}
如果你愿意,我有一个 class 包装了所有这些我可以发给你。从来没有真正把它打磨到足以用它做一个豆荚,但如果需要的话我确实有它。
特别感谢@rmaddy
检测屏幕尺寸的正确方法是:
NSLog(@"%f",[UIScreen mainScreen].bounds.size.height);
现在,如果您的应用程序以 Portrait
模式运行,您只需使用此代码即可检测 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
这是一个无论设备方向如何都有效的检查:
- (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;
}
请注意,这仅适用于 iOS8+
感谢@Mc.Lover
Portrait
和 Landscape
方向的一点更新:
if (([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && ([UIScreen mainScreen].bounds.size.height == 1366 || [UIScreen mainScreen].bounds.size.width == 1366))) {
//iPad Pro
}
Swift D1mers0n 解决方案的版本。
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
}
检测 iPad pro 12.9 inch 无论设备方向如何
#define iPadPro12 (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad && UIScreen.mainScreen.nativeBounds.size.height == 2732)
Swift 3 Justin Domnitz 的解决方案对 D1mers0n 的解决方案的版本:
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
}
遵循 Swift3 standards/practices,更好的方法是:
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
}
}
将此添加到 UIDevice
的 extension
允许您调用,这很整洁:
UIDevice.current.isiPadPro12
有一天 Apple 终于会给我们一个 enum
值!
这是我用的,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)
}
}
Swift3+
的固定解
有两种解决方案,第一种我在我的项目中使用过,你可以使用其中的任何一种。
1- 使用宏
2- 使用扩展
使用宏
#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
}
}
我在 Objective-C 中使用宏的解决方案:
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)))
我正在尝试检测 iPad Pro 设备,尝试通过以下方式猜测其高度:
NSLog(@"%f",self.view.frame.size.height);
但是returns1024
!与 iPad 非视网膜设备相同。有什么建议吗?
我需要用这行代码为 iPad Pro 指定一些代码:
#define iPadPro ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && [UIScreen mainScreen].bounds.size.height == 2732)
...并且即使在 iOS 模拟器上,代码也必须检测到 iPad Pro!
谢谢
已编辑:
有些人建议使用 LunchScreen ,但是当我使用它时会发生这种情况(按比例缩小):
这不是最好的长期解决方案,但今天对您有用...
获取设备的硬件字符串...
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);
将硬件字符串与这些硬件字符串进行比较:
if ([hardwareString isEqualToString:@"iPad6,7"] || [hardwareString isEqualToString:@"iPad6,8"]) {
// iPad Pro
}
如果你愿意,我有一个 class 包装了所有这些我可以发给你。从来没有真正把它打磨到足以用它做一个豆荚,但如果需要的话我确实有它。
特别感谢@rmaddy
检测屏幕尺寸的正确方法是:
NSLog(@"%f",[UIScreen mainScreen].bounds.size.height);
现在,如果您的应用程序以 Portrait
模式运行,您只需使用此代码即可检测 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
这是一个无论设备方向如何都有效的检查:
- (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;
}
请注意,这仅适用于 iOS8+
感谢@Mc.Lover
Portrait
和 Landscape
方向的一点更新:
if (([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && ([UIScreen mainScreen].bounds.size.height == 1366 || [UIScreen mainScreen].bounds.size.width == 1366))) {
//iPad Pro
}
Swift D1mers0n 解决方案的版本。
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
}
检测 iPad pro 12.9 inch 无论设备方向如何
#define iPadPro12 (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad && UIScreen.mainScreen.nativeBounds.size.height == 2732)
Swift 3 Justin Domnitz 的解决方案对 D1mers0n 的解决方案的版本:
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
}
遵循 Swift3 standards/practices,更好的方法是:
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
}
}
将此添加到 UIDevice
的 extension
允许您调用,这很整洁:
UIDevice.current.isiPadPro12
有一天 Apple 终于会给我们一个 enum
值!
这是我用的,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)
}
}
Swift3+
的固定解有两种解决方案,第一种我在我的项目中使用过,你可以使用其中的任何一种。
1- 使用宏
2- 使用扩展
使用宏
#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
}
}
我在 Objective-C 中使用宏的解决方案:
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)))