DeviceDetector

1.0.4

Detecting Apple devices
ShawnBaek/DeviceDetector

What's New

Add convenience init

2022-05-16T05:11:31Z

Add convenience init
Fix TestCases

DeviceDetector

DeviceDetector detects apple's devices(iPhone and iPad) model names 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 deviceSet: DeviceSet
    public let deviceName: String
    public let isiPad: Bool
    public let isiPhone: Bool
    public let hasSafeArea: Bool
    private let deviceDict: NSDictionary
    private init() {
        if let appleDevices = Bundle.module.path(forResource: "Device", ofType: "plist"),
           let dict = NSDictionary(contentsOfFile: appleDevices) {
            deviceDict = dict
        }
        else {
            deviceDict = [:]
        }
        deviceName = UIDevice.current.deviceName(dict: deviceDict) ?? ""
        deviceSet = UIDevice.current.device(name: deviceName)
        isiPad = deviceSet.isSubset(of: .iPadSet)
        isiPhone = deviceSet.isSubset(of: .iPhoneSet)
        if isiPhone, deviceSet.isSubset(of: .iPhoneSafeAreaSet) {
            hasSafeArea = true
        }
        else {
            hasSafeArea = false
        }
    }
    
    public func device(id: String) -> DeviceSet {
        guard let deviceName = UIDevice.current.deviceName(id: id, dict: deviceDict) else {
            return .unrecognized
        }
        return UIDevice.current.device(name: deviceName)
    }
}

Sample

test3 test4 test1 test2
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 deviceSet = detector.deviceSet
        
        let information = """
        Model: \(deviceName)
        iPhone?: \(detector.isiPhone)
        iPad?: \(detector.isiPad)
        Notch?: \(detector.hasSafeArea)
        
        4inch?: \(deviceSet.isSubset(of: .iPhone4inchSet))
        4.7inch?: \(deviceSet.isSubset(of: .iPhone4_7inchSet))
        iPhoneSE?: \(deviceSet.isSubset(of: .iPhoneSESet))
        iPhonePlus?: \(deviceSet.isSubset(of: .iPhonePlusSet))
        iPadPro?: \(deviceSet.isSubset(of: .iPadProSet))
        """
        label.text = information
    }
}

Features

You can check the device model not only physical device but also simulator.

  1. Check the current device
import DeviceDetector
DeviceDetector.current.deviceSet //DeviceSet.iPhone11
  1. Check the current device name (String)
import DeviceDetector
DeviceDetector.current.deviceName //iPhone11
  1. Check whether iPhone or iPad
import DeviceDetector
DeviceDetector.current.isiPhone //true or false
DeviceDetector.current.isiPad //true or false
  1. Device Groups It uses OptionSet. You can check Is your model subset of the device groups.
  • iPhoneSESet (iPhoneSE1, iPhoneSE2, iPhoneSE3)
import DeviceDetector
if DeviceDetector.current.deviceSet.isSubset(of: .iPhoneSESet) {
  print("This is iPhoneSE")
}
  • iPhonePlusSet (iPhone6Plus, iPhone7Plus, iPhone8Plus)
import DeviceDetector
if DeviceDetector.current.deviceSet.isSubset(of: .iPhonePlusSet) {
  print("This is iPhonePlus")
}
  • iPhone4_7inchSet (iPhoneSE2, iPhoneSE3, iPhone6, iPhone7, iPhone8)
import DeviceDetector
if DeviceDetector.current.deviceSet.isSubset(of: .iPhone4_7inchSet) {
  print("This is 4.7inch")
}
  • iPhone4inchSet (iPhoneSE1)
import DeviceDetector
if DeviceDetector.current.deviceSet.isSubset(of: .iPhone4inchSet) {
  print("This is 4inch")
}
  • iPhoneSafeAreaSet
import DeviceDetector
//Option 1. Use DeviceSet.iPhoneSafeAreaSet
if DeviceDetector.current.deviceSet.isSubset(of: .iPhoneSafeAreaSet) {
  print("This iPhone has safeArea")
}

//Option 2. DeviceDetector.shared.hasSafeArea
DeviceDetector.current.hasSafeArea //true or false
  • iPadProSet (iPadPro9_7inch, iPadPro10_5inch, iPadPro11inch, iPadPro12_9inch)
import DeviceDetector
if DeviceDetector.current.deviceSet.isSubset(of: .iPadProSet) {
  print("This is iPad Pro")
}

Environment

Above iOS 13

How to install, SPM

Use Swift Package Manager

UnitTest (23 tests)

tests

TODO

Support Mac and Apple Watch

Description

  • Swift Tools 5.6.0
View More Packages from this Author

Dependencies

  • None
Last updated: Thu Mar 28 2024 21:16:22 GMT-0900 (Hawaii-Aleutian Daylight Time)