0

Is there any way to judge the iPhone is iPhoneX series(iPhone X XR XS XSmax)?

My way is:

#define iPhoneXSeries (([[UIApplication sharedApplication] statusBarFrame].size.height == 44.0f) ? (YES):(NO))

Is there any hidden trouble?

Or is there any better way?

Cristik
  • 30,989
  • 25
  • 91
  • 127
无夜之星辰
  • 5,426
  • 4
  • 25
  • 48
  • 1
    Possible duplicate of [How to determine the current iPhone/device model?](https://stackoverflow.com/questions/26028918/how-to-determine-the-current-iphone-device-model) – Larme Sep 21 '18 at 13:04
  • 1
    Also why would you need to? If you need it for the statusbar height then use the safe insets. – rckoenes Sep 21 '18 at 13:06
  • @rckoenes I need to know because iPhoneX series statusbar's height are immutable but other iPhone's statusbar's height can change for example when in-call. – 无夜之星辰 Sep 21 '18 at 13:14
  • 1
    Still you can use [`safeAreaInsets`](https://developer.apple.com/documentation/uikit/uiview/2891103-safeareainsets) to make you code more future prove. – rckoenes Sep 21 '18 at 13:16

2 Answers2

6

I need to know because iPhoneX series statusbar's height are immutable but other iPhone's statusbar's height can change

So you don't really need to know whether your app is running on an iPhone X-series phone at all — what you're really trying to find out is whether the status bar height can change. And I'll be that you don't really even care about the status bar so much as you want to know where you can put content in your view so that it will always be unobscured by the status bar and other system objects. Whether that's the case or not, you should make sure that you're asking the right question. Don't rely on the device model to tell you about features, and don't rely on particular features to tell you the device model.

iOS usually gives you a way to find out about the features you need. If your goal is to keep your content visible, you should use UIView's safeAreaInsets property and also the safeAreaInsetsDidChange() method, which the system will call when the safe area changes (e.g. when the status bar height changes). You can then adjust your content to fit the new safe insets. Building your app this way means that you don't have to worry about your app breaking on new device models that have feature sets you don't expect, and you don't have to worry about future iOS updates undermining your assumptions.

Caleb
  • 124,013
  • 19
  • 183
  • 272
-2

Try this:

let size = UIScreen.mainScreen().bounds.size
print("Your device size: \(size)");

if size.height == 814 {
print("This is an iPhone X")
}
Mukesh
  • 777
  • 7
  • 20
  • 2
    Relying on screen size (or any other feature) to discern device type is a bad idea, just as relying on device type to discern features is a bad idea. – Caleb Sep 21 '18 at 13:56
  • FYI - the question is tagged Objective-C. Answers should be in the proper language. – rmaddy Sep 21 '18 at 17:49