What I'm trying to achieve is to set different actions based on their device type.
So for example: If I want to do different stuff for an iPhone 8
if device type is = iPhone 8
{
//action here
...
}
And so on for each device
What I'm trying to achieve is to set different actions based on their device type.
So for example: If I want to do different stuff for an iPhone 8
if device type is = iPhone 8
{
//action here
...
}
And so on for each device
Use this
struct DeviceType {
static let IS_IPHONE_4_OR_LESS = UIDevice.current.userInterfaceIdiom ==
.phone
&& ScreenSize.SCREEN_MAX_LENGTH < 568.0
static let IS_IPHONE_5 = UIDevice.current.userInterfaceIdiom == .phone &&
ScreenSize.SCREEN_MAX_LENGTH == 568.0
static let IS_IPHONE_6 = UIDevice.current.userInterfaceIdiom == .phone &&
ScreenSize.SCREEN_MAX_LENGTH == 667.0
static let IS_IPHONE_6S = UIDevice.current.userInterfaceIdiom == .phone &&
ScreenSize.SCREEN_MAX_LENGTH == 667.0
static let IS_IPHONE_7 = UIDevice.current.userInterfaceIdiom == .phone &&
ScreenSize.SCREEN_MAX_LENGTH == 667.0
static let IS_IPHONE_8 = UIDevice.current.userInterfaceIdiom == .phone &&
ScreenSize.SCREEN_MAX_LENGTH == 667.0
static let IS_IPHONE_6P = UIDevice.current.userInterfaceIdiom == .phone &&
ScreenSize.SCREEN_MAX_LENGTH == 736.0
static let IS_IPHONE_X = UIDevice.current.userInterfaceIdiom == .phone &&
ScreenSize.SCREEN_MAX_LENGTH == 812.0
static let IS_IPHONE_XR = UIDevice.current.userInterfaceIdiom == .phone &&
ScreenSize.SCREEN_MAX_LENGTH == 896.0
static let IS_IPAD = UIDevice.current.userInterfaceIdiom == .pad &&
ScreenSize.SCREEN_MAX_LENGTH == 1024.0
static let IS_IPAD_PRO = UIDevice.current.userInterfaceIdiom == .pad &&
ScreenSize.SCREEN_MAX_LENGTH == 1366.0
}
I think you shoud use this library:
https://github.com/lmirosevic/GBDeviceInfo
With this library you can write the code you asked for and you get tons of other features (obtaining device information) for free
if (deviceInfo.model == GBDeviceModeliPhone6) {
print("It's an iPhone 6")
}
well you can do different stuff by using this below code by using it in your class so, you can set set action accordingly :=
struct Device {
// iDevice detection code
static let IS_IPAD = UIDevice.current.userInterfaceIdiom == .pad
static let IS_IPHONE = UIDevice.current.userInterfaceIdiom == .phone
static let IS_RETINA = UIScreen.main.scale >= 2.0
static let SCREEN_WIDTH = Int(UIScreen.main.bounds.size.width)
static let SCREEN_HEIGHT = Int(UIScreen.main.bounds.size.height)
static let SCREEN_MAX_LENGTH = Int( max(SCREEN_WIDTH, SCREEN_HEIGHT) )
static let SCREEN_MIN_LENGTH = Int( min(SCREEN_WIDTH, SCREEN_HEIGHT) )
static let IS_IPHONE_4_OR_LESS = IS_IPHONE && SCREEN_MAX_LENGTH < 568
static let IS_IPHONE_5 = IS_IPHONE && SCREEN_MAX_LENGTH == 568
static let IS_IPHONE_6 = IS_IPHONE && SCREEN_MAX_LENGTH == 667
static let IS_IPHONE_7 = IS_IPHONE && SCREEN_MAX_LENGTH == 667
static let IS_IPHONE_8 = IS_IPHONE && SCREEN_MAX_LENGTH == 667
static let IS_IPHONE_6P = IS_IPHONE && SCREEN_MAX_LENGTH == 736
static let IS_IPHONE_X = IS_IPHONE && SCREEN_MAX_LENGTH == 812
}
now you can set constrain like this:==
if(Device.IS_IPHONE_5 || Device.IS_IPHONE_4_OR_LESS){
//--- set your constrain for iphone 5 and 4
}else if(Device.IS_IPAD){
//--- set your constrain for ipad
}else{
//--- set default constrain
}
if UIDevice.current.deviceCategory() == .iPhoneX || UIDevice.current.deviceCategory() == .iPhoneXR {
// Just this checking wherever you want
}
Add extension below then use it.
extension UIDevice {
enum DeviceCategory {
case iPhone4
case iPhone5
case iPhone6
case iPhone6Plus
case iPhoneX
case iPhoneXR
case iPadPro12inch
case iPad
}
func deviceCategory() -> DeviceCategory {
let height = UIScreen.main.bounds.size.height
switch height {
case 480:
return .iPhone4
case 568:
return .iPhone5
case 667:
return .iPhone6
case 736:
return .iPhone6Plus
case 812:
return .iPhoneX
case 896:
return .iPhoneXR
case 1366:
return .iPadPro12inch
default:
return .iPad
}
}
}