Created
October 28, 2019 08:20
-
-
Save heitara/c3c761e0416be04f6143280ed8f447d6 to your computer and use it in GitHub Desktop.
Distinguish different iPhones based on the device screen height.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIDevice+Extensions.swift | |
// tenbet | |
// | |
// Created by Emil Atanasov on 28.10.19. | |
// Copyright © 2019. All rights reserved. | |
// | |
//based on https://stackoverflow.com/questions/46192280/detect-if-the-device-is-iphone-x/47067296 | |
///Extension that can be used to distinguish different types of iPhones | |
///iPad devices are treated as one - generic Pad | |
public extension UIDevice { | |
enum DeviceType { | |
case iPad | |
case iPhone_unknown | |
case iPhone_5_5S_5C_SE | |
case iPhone_6_6S_7_8 | |
case iPhone_6_6S_7_8_PLUS | |
case iPhone_X_Xs_11_Pro | |
case iPhone_Xs_Max_11_Pro_Max | |
case iPhone_Xr_11 | |
} | |
/// Returns true if the harware has a home button | |
var hasHomeButton: Bool { | |
switch deviceType { | |
case .iPhone_X_Xs_11_Pro, .iPhone_Xr_11, .iPhone_Xs_Max_11_Pro_Max: | |
return false | |
default: | |
return true | |
} | |
} | |
/// Return the type device type, based on screen height | |
var deviceType: DeviceType { | |
if userInterfaceIdiom == .phone { | |
switch UIScreen.main.nativeBounds.height { | |
case 1136: | |
return .iPhone_5_5S_5C_SE | |
case 1334: | |
return .iPhone_6_6S_7_8 | |
case 1920, 2208: | |
return .iPhone_6_6S_7_8_PLUS | |
case 2436: | |
return .iPhone_X_Xs_11_Pro | |
case 2688: | |
return .iPhone_Xs_Max_11_Pro_Max | |
case 1792: | |
return .iPhone_Xr_11 | |
default: | |
return .iPhone_unknown | |
} | |
} | |
return .iPad | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment