1

I'm trying to detect if the current device is an iPhone X in Objective-C. I've looked at some other posts that do this in Swift and a proper method seems to be to compare the safe area insets with UIEdgeInsets.zero. However, whenever I attempt to do this, I am getting nil. I am running this on iOS 11 and the iPhone X simulator.

When I step through this method - I enter into the first clause, but still get a nil returned.

- (BOOL)isiPhoneX
{   
    if (@available(iOS 11.0, *))
    {
        return UIEdgeInsetsEqualToEdgeInsets(self.safeAreaInsets, UIEdgeInsetsZero);
    }
    else
    {
        return nil;
    }
}
Brandon Lee
  • 194
  • 2
  • 12
  • You aren't getting `nil`, you are getting `NO`. `NO` may appear as `nil` depending on how you are looking at the value. – rmaddy Nov 14 '17 at 00:10
  • 1
    And your logic is backwards. For an iPhone X, the safe area insets will *not* be zero. And don't return `nil` for `BOOL`. Return `NO` in the `else`. – rmaddy Nov 14 '17 at 00:11
  • 1
    The link has the answer. The only ultimate way to track device is using `UIDevice`. [Check this](https://stackoverflow.com/a/46316292/1215715) – Ryan Nov 14 '17 at 00:11
  • 2
    BTW - why do you need to know whether the device is an iPhone X? Your app shouldn't care if written properly. – rmaddy Nov 14 '17 at 00:12

0 Answers0