DeviceDetector detects apple’s devices(iPhone and iPad) model by using identifier code. I referred to this wiki. It does not support old models that can’t install iOS 13
public final class DeviceDetector {
public static let current = DeviceDetector()
public let device: DeviceOptionSet
public let deviceName: String
public let isiPad: Bool
public let isiPhone: Bool
public let hasSafeArea: Bool
private let deviceDict: NSDictionary
private init(identifier: String? = nil) {
if let appleDevices = Bundle.module.path(forResource: "Device", ofType: "plist"),
let dict = NSDictionary(contentsOfFile: appleDevices) {
deviceDict = dict
}
else {
deviceDict = [:]
}
deviceName = UIDevice.current.deviceName(id: identifier, dict: deviceDict) ?? ""
device = UIDevice.current.device(name: deviceName)
isiPad = device.isSubset(of: .iPadSet)
isiPhone = device.isSubset(of: .iPhoneSet)
if isiPhone, device.isSubset(of: .iPhoneSafeAreaSet) {
hasSafeArea = true
}
else {
hasSafeArea = false
}
}
public convenience init?(id identifier: String) {
self.init(identifier: identifier)
}
}
Sample
import UIKit
import DeviceDetector
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let detector = DeviceDetector.current
let deviceName = detector.deviceName
let device = detector.device
let information = """
Model: \(deviceName)
iPhone?: \(detector.isiPhone)
iPad?: \(detector.isiPad)
Notch?: \(detector.hasSafeArea)
4inch?: \(device.isSubset(of: .iPhone4inchSet))
4.7inch?: \(device.isSubset(of: .iPhone4_7inchSet))
iPhoneSE?: \(device.isSubset(of: .iPhoneSESet))
iPhonePlus?: \(device.isSubset(of: .iPhonePlusSet))
iPadPro?: \(device.isSubset(of: .iPadProSet))
"""
label.text = information
}
}
Features
You can check the device model not only physical device but also simulator.
DeviceDetector
DeviceDetector detects apple’s devices(iPhone and iPad) model by using identifier code. I referred to this wiki. It does not support old models that can’t install iOS 13
Sample
Features
You can check the device model not only physical device but also simulator.
Check the current device
Check the current device name (String)
Check whether iPhone or iPad
Device Groups It uses OptionSet. You can check Is your model subset of the device groups.
iPhoneSESet (iPhoneSE1, iPhoneSE2, iPhoneSE3)
iPhonePlusSet (iPhone6Plus, iPhone7Plus, iPhone8Plus)
iPhone4_7inchSet (iPhoneSE2, iPhoneSE3, iPhone6, iPhone7, iPhone8)
iPhone4inchSet (iPhoneSE1)
iPhoneSafeAreaSet ```swift import DeviceDetector //Option 1. Use DeviceSet.iPhoneSafeAreaSet if DeviceDetector.current.device.isSubset(of: .iPhoneSafeAreaSet) { print(“This iPhone has safeArea”) }
//Option 2. DeviceDetector.shared.hasSafeArea DeviceDetector.current.hasSafeArea //true or false
Environment
Above iOS 13
How to install, SPM
Use Swift Package Manager
UnitTest (23 tests)
TODO
Support Mac and Apple Watch