设备检测器

DeviceDetector 使用识别码检测苹果设备(iPhone 和 iPad)型号。我参考了这个 wiki。它不支持无法安装 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)
    }
}

示例

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 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
    }
}

特性

您不仅可以检查物理设备,还可以检查模拟器的设备型号。

  1. 检查当前设备
import DeviceDetector
DeviceDetector.current.device //DeviceOptionSet.iPhone11
  1. 检查当前设备名称 (字符串)
import DeviceDetector
DeviceDetector.current.deviceName //iPhone11
  1. 检查是否为 iPhone 或 iPad
import DeviceDetector
DeviceDetector.current.isiPhone //true or false
DeviceDetector.current.isiPad //true or false
  1. 设备组 它使用 OptionSet。您可以检查您的型号是否是设备组的子集。
import DeviceDetector
if DeviceDetector.current.device.isSubset(of: .iPhoneSESet) {
  print("This is iPhoneSE")
}
import DeviceDetector
if DeviceDetector.current.device.isSubset(of: .iPhonePlusSet) {
  print("This is iPhonePlus")
}
import DeviceDetector
if DeviceDetector.current.device.isSubset(of: .iPhone4_7inchSet) {
  print("This is 4.7inch")
}
import DeviceDetector
if DeviceDetector.current.device.isSubset(of: .iPhone4inchSet) {
  print("This is 4inch")
}
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
import DeviceDetector
if DeviceDetector.current.device.isSubset(of: .iPadProSet) {
  print("This is iPad Pro")
}

环境

iOS 13 及以上

如何安装,SPM

使用 Swift Package Manager

单元测试 (23 个测试)

tests

TODO

支持 Mac 和 Apple Watch