How to programmatically identify if Apple iOS (iPhone) device has a notch or safe area

Since iPhone X, Apple is releasing various sizes of iPhone with a notch.

So Identifying new iPhones that have a notch or doesn’t is a new problem for every Apple App Store App Developer. e.g. Apple has just released in 2020 with iPhone 12, Pro, Pro Max and Mini that are not similar in screen dimensions of previous generations of iPhone 11.

After reading and surfing the web I found two helpful articles on the web that will guide you to solve this problem one post was on Stack Overflow and another article by @cafielo.

But code shared in above both articles were not compatible with new Apple iOS versions 13 and 14.

So I rewrote those code that may be compatible with Apple iOS 13 and 14 operating system.

Step 01: First declare this code as an UIDevice extension in the root of your Swift file i.e. Public Declaration

extension UIDevice {
    var hasNotch: Bool {
        let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
        if keyWindow?.safeAreaInsets.bottom ?? 0 > 0 {
            return true
        } else {
            return false
        }
    }
}

Step 02: Now use this code get the boolean answer to identify if the iPhone has Notch or not

if UIDevice.current.hasNotch {
    print("device with notch")

} else {
    print("device without notch")

}

I hope this tutorial helps you to solve your problem.

Thanks & Regards
Mandar Apte

Leave a Reply

Your email address will not be published. Required fields are marked *