iPhone机型判断(新增iPhone 11、iPhone 11 Pro、iPhone 11 Pro Max)

闲来无事,把项目中写的iPhone系列设备判断代码copy出来以供各位开发兄弟参考参考。
设备型号官网查询地址

import Foundation

extension UIDevice {
    
    @objc public enum Name: Int {
        case unknown
        case iPhone4
        case iPhone4s
        case iPhone5
        case iPhone5c
        case iPhone5s
        case iPhone6
        case iPhone6Plus
        case iPhone6s
        case iPhone6sPlus
        case iPhone7
        case iPhone7Plus
        case iPhoneSE
        case iPhone8
        case iPhone8Plus
        case iPhoneX
        case iPhoneXs
        case iPhoneXsMax
        case iPhoneXr
        case iPhone11
        case iPhone11Pro 
        case iPhone11ProMax

        case iPad2
        case iPad3
        case iPad4
        case iPadAir
        case iPadAir2
        case iPad5
        case iPad6
        case iPadMini
        case iPadMini2
        case iPadMini3
        case iPadMini4
        case iPadPro
    }
    
    @objc public static var name: Name = {
        return identifierToType(identifier: UIDevice.identifier)
    }()
    
    static func identifierToType(identifier: String) -> Name {
        switch identifier {
        case "iPhone3,1", "iPhone3,2", "iPhone3,3": return .iPhone4
        case "iPhone4,1": return .iPhone4s
        case "iPhone5,1", "iPhone5,2": return .iPhone5
        case "iPhone5,3", "iPhone5,4": return .iPhone5c
        case "iPhone6,1", "iPhone6,2": return .iPhone5s
        case "iPhone7,2": return .iPhone6
        case "iPhone7,1": return .iPhone6Plus
        case "iPhone8,1": return .iPhone6s
        case "iPhone8,2": return .iPhone6sPlus
        case "iPhone9,1", "iPhone9,3": return .iPhone7
        case "iPhone9,2", "iPhone9,4": return .iPhone7Plus
        case "iPhone8,4": return .iPhoneSE
        case "iPhone10,1", "iPhone10,4": return .iPhone8
        case "iPhone10,2", "iPhone10,5": return .iPhone8Plus
        case "iPhone10,3", "iPhone10,6": return .iPhoneX
        case "iPhone11,2": return .iPhoneXs
        case "iPhone11,4", "iPhone11,6": return .iPhoneXsMax
        case "iPhone11,8": return .iPhoneXr
        case "iPhone12,1": return .iPhone11
        case "iPhone12,3": return .iPhone11Pro
        case "iPhone12,5": return .iPhone11ProMax
        case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4": return .iPad2
        case "iPad3,1", "iPad3,2", "iPad3,3": return .iPad3
        case "iPad3,4", "iPad3,5", "iPad3,6": return .iPad4
        case "iPad4,1", "iPad4,2", "iPad4,3": return .iPadAir
        case "iPad5,3", "iPad5,4": return .iPadAir2
        case "iPad6,11", "iPad6,12": return .iPad5
        case "iPad7,5", "iPad7,6": return .iPad6
        case "iPad2,5", "iPad2,6", "iPad2,7": return .iPadMini
        case "iPad4,4", "iPad4,5", "iPad4,6": return .iPadMini2
        case "iPad4,7", "iPad4,8", "iPad4,9": return .iPadMini3
        case "iPad5,1", "iPad5,2": return .iPadMini4
        case "iPad6,3", "iPad6,4", "iPad6,7", "iPad6,8", "iPad7,1", "iPad7,2", "iPad7,3", "iPad7,4", "iPad8,1", "iPad8,2", "iPad8,3", "iPad8,4", "iPad8,5", "iPad8,6", "iPad8,7", "iPad8,8": return .iPadPro
        case "i386", "x86_64": return identifierToType(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "unknow")
        default:
            return .unknown
        }
    }
    
    /// 获取系统标识,用来判断手机型号
    public static var identifier: String = {
        var systemInfo = utsname()
        uname(&systemInfo)
        let mirror = Mirror(reflecting: systemInfo.machine)
        
        let identifier = mirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        return identifier
    }()
    
    /// 是否是x系列的手机,全面屏
    @objc public static var isXSeries: Bool = {
        let xSeries = [Name.iPhoneX, Name.iPhoneXs, Name.iPhoneXsMax, Name.iPhoneXr,Name.iPhone11,Name.iPhone11Pro,Name.iPhone11ProMax]
        if xSeries.contains(UIDevice.name) {
            return true
        }
        
        return false
    }()
    
    /// 是否是ipad
    @objc public static var isPad: Bool = {
        let pads = [Name.iPad2, Name.iPad3, Name.iPad4, Name.iPad5, Name.iPad6, Name.iPadMini, Name.iPadMini2, Name.iPadMini3, Name.iPadMini4, Name.iPadAir, Name.iPadAir2, Name.iPadPro]
        
        if pads.contains(UIDevice.name) {
            return true
        }
        
        return false
    }()
    
    /// 底部安全margin
    @objc public static var bottomMargin: CGFloat = {
        if UIDevice.isXSeries {
            return 34.0
        }
        
        return 0.0
    }()
    
    /// 顶部状态栏高度
    @objc public static var topMargin: CGFloat = {
        if UIDevice.isXSeries {
            return 44.0
        }
        
        return 20.0
    }()
    
    /// 判断是否是模拟器
    @objc public static var isSimulator: Bool = {
        let iden = UIDevice.identifier
        if iden == "i386" || iden == "x86_64" {
            return true
        }
        
        return false
    }()
}

你可能感兴趣的:(iPhone机型判断(新增iPhone 11、iPhone 11 Pro、iPhone 11 Pro Max))