Musa almatri
Musa almatri

Reputation: 5842

How to detect if the device (iphone) has physical home button?

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 ?

Upvotes: 16

Views: 12502

Answers (3)

SM Abu Taher Asif
SM Abu Taher Asif

Reputation: 2331

iOS version >= 13.0

UIApplication.shared.windows is deprecated from iOS 15.0, we can replace it like below:

    var hasHomeButton: Bool {
        let window = UIApplication
            .shared
            .connectedScenes
            .compactMap { $0 as? UIWindowScene }
            .flatMap { $0.windows }
            .first { $0.isKeyWindow }
        guard let safeAreaBottom =  window?.safeAreaInsets.bottom else {
            return false
        }
        return safeAreaBottom <= 0
    }

In SwiftUI you can also use this variable, but don't use it from init() of your SwiftUI view struct.

Upvotes: 0

Vlad Bruno
Vlad Bruno

Reputation: 377

For iOS 13 and higher:

    var isBottom: Bool {
    if #available(iOS 13.0, *), UIApplication.shared.windows[0].safeAreaInsets.bottom > 0 {
        return true
    }
    return false
}

Upvotes: 6

Jake
Jake

Reputation: 13753

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.

Upvotes: 36

Related Questions