Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify iPhones with Notch Programmatically

I am trying to identify iPhones with the notch programmatically. I am aware of the screen.size method, but when you have a universal app supporting all interface orientations, it becomes a mess (counting all the possible variations). So I am looking for a simpler and more elegant way to detect the newer iPhones X models.

I have stumbled upon a method online that is supposed to work. In it, you measure the bottom inset of the safe area and if it is not zero, you have an iPhone X model. This makes sense in theory since, the safe area does not reach the bottom of the screen on the iPhone X handsets but does on all other devices. I do the check this way:

if (@available( iOS 11.0, * )) {
    if ([[[UIApplication sharedApplication] keyWindow] safeAreaInsets].bottom > 0) {
        // iPhone with notch
    }
} else {
    // Regular iPhone
}

This, however, does not work. Can someone point out my mistake in the implementation or confirm if this method is even viable?

like image 676
Gergely Kovacs Avatar asked Apr 16 '19 19:04

Gergely Kovacs


People also ask

What is the notch for on iPhone?

The notch, which Apple calls the TrueDepth camera array, is the bit of black empty space that surrounds the front camera. It's been a pretty controversial design decision ever since it was introduced in 2017. Now, it's a hole punch design that's also dynamic.

How many pixels is the notch on iPhone?

On iPhone Notch Sizes – 512 Pixels.

How do I find my current iPhone model?

Go to Settings > General > About. To the right of Model, you'll see the part number. To see the model number, tap the part number.

How do I find the device name in swift 5?

Basic Swift Code for iOS Apps Device name is generally a name which will find in the device Setting→ General→ About.


2 Answers

I had to dig around quite a bit but found the answer (I'd like to shout out to user6788419 whose right answer was berried deep in another thread).

First of all, the above code is correct. Checking if the safe area's bottom inset is larger than zero will accurately identify iPhones with a notch (as of this writing). So, this is correct:

if (@available( iOS 11.0, * )) {
    if ([[[UIApplication sharedApplication] keyWindow] safeAreaInsets].bottom > 0) {
        // iPhone with notch
    }
}

However, it matters where you put the above statement in your code because UIWindow is not available until the first run loop has concluded. That means, if you check the notch in your viewDidLoad or before it concluded (in your init for example), the bottom inset will always be zero.

If you, similarly to me, need this check to setup your main view, you can just move all your setup into separate function (such as postViewDidLoad) and call it once viewDidLoad has concluded:

[self performSelector:@selector(postViewDidLoad) withObject:nil afterDelay:0.0f];

Or, alternatively, you enclose it:

dispatch_async(dispatch_get_main_queue(), ^{
    // Check notch here
});

UPDATE: code for iOS 13 and up (does the same thing). "areaPosition" is a string argument when you call the function. "top" checks for the notch, everything else checks for the presence of the bottom home indicator.

- (UIWindow *) keyWindow {
    UIWindow *foundWindow   = nil;
    NSArray *windows        = [[UIApplication sharedApplication]windows];
    for (UIWindow *window in windows) {
        if (window.isKeyWindow) {
            foundWindow     = window;
            break;
        }
    }
    return foundWindow;
}

- (BOOL) checkSafeArea:(NSString *)areaPosition {
   if (@available(iOS 13.0, *)) {
       if ([areaPosition isEqualToString:@"top"]) {
           return [self keyWindow].safeAreaInsets.top > 20.0f;
       } else {
           return [self keyWindow].safeAreaInsets.bottom > 0.0f;
       }
   } else {
       if ([areaPosition isEqualToString:@"top"]) {
           return [[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0f;
       } else {
           return [[[UIApplication sharedApplication] delegate] window].safeAreaInsets.bottom > 0.0f;
       }
   }
   return  NO;
}
like image 73
Gergely Kovacs Avatar answered Oct 17 '22 11:10

Gergely Kovacs


Swift 5 and (iOS12 or Above)

var isNotch: Bool {
   return (UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0) > 0
}
like image 35
Anand Khanpara Avatar answered Oct 17 '22 13:10

Anand Khanpara