我的iOS应用程序为UINavigationBar使用了自定义高度,这在新的iPhone X上导致了一些问题。

是否有人已经知道如何通过编程(在Objective-C中)可靠地检测应用程序是否在iPhone X上运行?

编辑:

当然,检查屏幕的大小是可能的,但是,我想知道是否有一些“内置”的方法,如TARGET_OS_IPHONE来检测iOS…

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    if (screenSize.height == 812)
        NSLog(@"iPhone X");
}

编辑2:

我不认为,我的问题是一个重复的关联问题。当然,有一些方法可以“测量”当前设备的不同属性,并使用结果来决定使用哪种设备。然而,这并不是我在第一篇编辑中试图强调的问题的实际意义。

真正的问题是:“是否有可能直接检测当前设备是否是iPhone X(例如通过某些SDK功能),还是我必须使用间接测量?”

根据目前给出的答案,我假设答案是“不,没有直接的方法。测量是要走的路。”


当前回答

对于那些获得2001px而不是2436px的原生边界高度的人(比如我),这是因为你使用的是iOS 11 (Xcode 8而不是Xcode 9)之前的旧SDK构建的应用程序。使用旧SDK, iOS将在iPhone X上显示应用程序的“黑盒子”,而不是将屏幕边缘扩展到顶部的“传感器缺口”。这减少了屏幕大小,这就是为什么该属性返回2001而不是2436。

如果您只对设备检测感兴趣,最简单的解决方案是检查两个大小。我使用这种方法来检测FaceID,同时使用一个旧的Xcode SDK,它没有ENUM值指定生物特征类型。在这种情况下,使用屏幕高度进行设备检测似乎是知道设备是否具有FaceID还是TouchID的最佳方法,而无需更新Xcode。

其他回答

根据@saswanb的回复,这是Swift 4的版本:

var iphoneX = false
if #available(iOS 11.0, *) {
    if ((UIApplication.shared.keyWindow?.safeAreaInsets.top)! > CGFloat(0.0)) {
        iphoneX = true
    }
}

检测设备是否为iPhone X的最佳和最简单的方法是,

https://github.com/stephanheilner/UIDevice-DisplayName

var systemInfo = utsname()
uname(&systemInfo)
let machineMirror = Mirror(reflecting: systemInfo.machine)
let identifier = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8 , value != 0 else { return identifier}
            return identifier + String(UnicodeScalar(UInt8(value)))}

标识符是iPhone10,3或者iPhone10,6对于iPhone X。

所有这些基于尺寸的答案都很容易在未来的设备上出现错误行为。它们今天还能用,但如果明年有一款同样大小的iPhone,但在玻璃下面有摄像头等,所以没有“缺口”呢?如果唯一的选择是更新应用程序,那么这对你和你的客户来说都是一个糟糕的解决方案。

您还可以检查硬件型号字符串,如“iPhone10,1”,但这是有问题的,因为有时苹果会为世界各地的不同运营商发布不同的型号。

正确的方法是重新设计顶部布局,或者解决自定义导航栏高度的问题(这是我关注的重点)。但是,如果你决定不做这两件事中的任何一件,你要意识到无论你现在做什么都是为了让它工作,你需要在某个时候纠正它,也许是多次,以保持它工作。

struct ScreenSize {
    static let width = UIScreen.main.bounds.size.width
    static let height = UIScreen.main.bounds.size.height
    static let maxLength = max(ScreenSize.width, ScreenSize.height)
    static let minLength = min(ScreenSize.width, ScreenSize.height)
    static let frame = CGRect(x: 0, y: 0, width: ScreenSize.width, height: ScreenSize.height)
}

struct DeviceType {
    static let iPhone4orLess = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.maxLength < 568.0
    static let iPhone5orSE = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.maxLength == 568.0
    static let iPhone678 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.maxLength == 667.0
    static let iPhone678p = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.maxLength == 736.0
    static let iPhoneX = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.maxLength == 812.0

    static let IS_IPAD              = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.maxLength == 1024.0
    static let IS_IPAD_PRO          = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.maxLength == 1366.0
}

如何检测iOS设备型号和屏幕大小?

CheckDevice检测当前的设备型号和屏幕大小。

你也可以使用

CheckDevice.size () iPhone 12 mini的。screen5_4inch

等等…… 也许……

CheckDevice.isPhone ()

检查设备类型为iPhone。

CheckDevice isWatch()。

CheckDevice isSimulator()。

CheckDevice isPad()。

伟大的回购

CheckDevice https://github.com/ugurethemaydin/CheckDevice