15

How to detect in Swift if the current device (iPhone) has a physical home button or hasn't, like: iPhone X, iPhone Xs, iPhone Xs Max, iPhone Xr ?

Musa almatri
  • 5,596
  • 2
  • 34
  • 33
  • 1
    No home button == safe area > 0 returns true – Kerberos Oct 04 '18 at 18:13
  • 6
    Out of curiosity, why do you wish to know if a device has a Home button or not? – rmaddy Oct 04 '18 at 18:14
  • Look at the accepted answer to this question: https://stackoverflow.com/questions/26028918/how-to-determine-the-current-iphone-device-model - maybe modify the extension to deal with the devices you're concerned about. – Jacob M. Barnard Oct 04 '18 at 18:15
  • This question shows how to tell what authentication a device supports, but again why? https://stackoverflow.com/questions/46887547/how-to-programatically-check-support-of-face-id-and-touch-id – CodeBender Oct 04 '18 at 18:53
  • 1
    Possible duplicate of [iOS: Detect if the device is iPhone X family (frameless)](https://stackoverflow.com/questions/52402477/ios-detect-if-the-device-is-iphone-x-family-frameless) – Cristik Oct 04 '18 at 19:16
  • @rmaddy, I wanted to design custom tab bar, but the iPhone X family devices have different tab bar style. I should ask about the iPhone X family devices, not about the physical home button XD – Musa almatri Oct 05 '18 at 09:28
  • What's different about the tab bar style? It looks the same to me as any other device. – rmaddy Oct 05 '18 at 15:01
  • @rmaddy the height for example – Musa almatri Oct 05 '18 at 16:07
  • 1
    That extra height is from the safe area. So you don't need to know the type of device. Just add the safe area. – rmaddy Oct 05 '18 at 16:09
  • @rmaddy you're totally right, I didn't ask the right question, thanks anyway – Musa almatri Oct 05 '18 at 16:19
  • 1
    Which was the point of my original comment. In most cases, when someone is looking to detect a specific device (or device type), they are doing it wrong. It's always best to focus a question on the actual task you are trying to solve. – rmaddy Oct 05 '18 at 16:22
  • this question is perfectly valid. In my case, it's because we want to indicate to less savvy users how to access the control panel (with a caveat that this could change over time and therefore not show it for future iOS version we don't know about.) – pulse4life Nov 20 '18 at 17:14
  • This question (and the answer!) was useful to me. Here's why I need to know: I want to emulate how the device Lock Screen looks and I need to know whether iOS will display "Press home to open" at the bottom or not. If there's a physical home button it does that, else not. – Ben May 04 '19 at 09:36
  • I'm afraid the "why" comments here are misguided. It is 100% normal that you need to adjust layouts depending on whether the "push-up bar" is there or not. One example of dozens is when you have a rounded element. Note, *Apple do this* in a number of their apps! By all means, devs should understand to "just use the safe area" where that is relevant, but you have to determine buttonless/notchy in almost all designs. – Fattie Feb 26 '20 at 21:36
  • Err, for a couple more years, until the ancient phones get lost! :) – Fattie Feb 26 '20 at 21:37

2 Answers2

33

Check the safe area:

if @available(iOS 11.0, *), 
    UIApplication.sharedApplication.keyWindow?.safeAreaInsets.bottom > 0 {
    return true
}
return false

Swift 4.2 version:-

var isBottom: Bool {
    if #available(iOS 11.0, *), let keyWindow = UIApplication.shared.keyWindow, keyWindow.safeAreaInsets.bottom > 0 {
        return true
    }
    return false
}

You can also check the device type (check out this post), but checking the safe area is probably the easiest way.

Mandeep Singh
  • 2,810
  • 1
  • 19
  • 31
Jake
  • 13,097
  • 9
  • 44
  • 73
  • 4
    Swift 4.2: ```if #available(iOS 11.0, *), let keyWindow = UIApplication.shared.keyWindow, keyWindow.safeAreaInsets.bottom > 0 { return true } return false``` – Adam Waite Mar 11 '19 at 13:42
  • I need to know whether the button exists very early as my layout depends on it, but at the time I query it keyWindow is null. Are there any other ways to find out about the buttons existence? – jho Aug 25 '22 at 10:56
4

For iOS 13 and higher:

    var isBottom: Bool {
    if #available(iOS 13.0, *), UIApplication.shared.windows[0].safeAreaInsets.bottom > 0 {
        return true
    }
    return false
}
Vlad Bruno
  • 327
  • 3
  • 7
  • Iphones which have hardware home buttons can have iOS >= 13 installed, so I believe this is not correct. – jho Aug 25 '22 at 10:59