How we can check model of iPhone (e.g iPhone 4/ iPhone5) in Swift. Using following code I can check whether it is iPhone of iPod.
let iphoneModel = UIDevice.currentDevice().model
if (iphoneModel == "iPhone"){
println("Iphone 4")
}
I need to know specifically which iPhone model it is.
Basic Swift Code for iOS AppsOpen Xcode → New Project and add the below code in ViewController's viewDidLoad method. UIDevice. current. model, gives you a model whether you have an iPhone, iPad, UIDevice.current.name gives you a name identifying the device.
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.
To detect current device with iOS/Swift we can use UserInterfaceIdiom. It is an enum in swift, which tells which device is being used. The interface idiom provides multiple values in it's enum which are.
A device model is the analytical expression developed based on theoretical and experimental study. Variables and constants that make up this Analytical Expression are model parameters. In other words, device parameters are used to reproduce the actual element characteristics on a simulator.
Swift 4 + iPhone Xr, Xs, Xs Max support
import UIKit
enum UIUserInterfaceIdiom : Int {
case Unspecified
case phone
case pad
}
struct ScreenSize {
static let screenWidth = UIScreen.main.bounds.size.width
static let screenHeight = UIScreen.main.bounds.size.height
static let screenMaxLength = max(ScreenSize.screenWidth, ScreenSize.screenHeight)
static let screenMinLength = min(ScreenSize.screenWidth, ScreenSize.screenHeight)
}
struct DeviceType {
static let iPhone4OrLess = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.screenMaxLength < 568.0
static let iPhoneSE = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.screenMaxLength == 568.0
static let iPhone8 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.screenMaxLength == 667.0
static let iPhone8Plus = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.screenMaxLength == 736.0
static let iPhoneXr = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.screenMaxLength == 896.0
static let iPhoneXs = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.screenMaxLength == 812.0
static let iPhoneXsMax = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.screenMaxLength == 896.0
static let iPad = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.screenMaxLength == 1024.0
}
And use :
if DeviceType.iPhoneXsMax {
print("is Max here")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With