iOS-判断设备型号(判断iPhoneX)

原文链接:https://stackoverflow.com/questions/26028918/how-to-determine-the-current-iphone-device-model/26962452#26962452
型号对照表:https://www.theiphonewiki.com/wiki/Models
下面是我整理过后写成的扩展,可直接Ctrl+C、Ctrl+V使用

import UIKit

public enum DeviceType: Int {
    case simulator
    case appleTV
    case appleTV4K
    case homePod

    case iPod1
    case iPod2
    case iPod3
    case iPod4
    case iPod5

    case iPad2
    case iPad3
    case iPad4
    case iPad5
    case iPadAir1
    case iPadAir2

    case iPadMini1
    case iPadMini2
    case iPadMini3
    case iPadMini4

    case iPadPro9_7
    case iPadPro12_9
    case iPadPro10_5

    case iPhone4
    case iPhone4S
    case iPhone5
    case iPhone5C
    case iPhone5S
    case iPhoneSE
    case iPhone6
    case iPhone6plus
    case iPhone6S
    case iPhone6Splus
    case iPhone7
    case iPhone7plus
    case iPhone8
    case iPhone8plus

    case iPhoneX
    case iPhoneXR
    case iPhoneXS
    case iPhoneXSMax

    case unrecognized
}

extension UIDevice {
    var modelName: String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }

        switch identifier {
        case "iPod1,1":                                  return "iPod Touch 1"
        case "iPod2,1":                                  return "iPod Touch 2"
        case "iPod3,1":                                  return "iPod Touch 3"
        case "iPod4,1":                                  return "iPod Touch 4"
        case "iPod5,1":                                  return "iPod Touch 5"
        case "iPod7,1":                                  return "iPod Touch 6"

        case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4": return "iPad 2"
        case "iPad3,1", "iPad3,2", "iPad3,3":            return "iPad 3"
        case "iPad3,4", "iPad3,5", "iPad3,6":            return "iPad 4"
        case "iPad6,11", "iPad6,12":                     return "iPad 5"
        case "iPad4,1", "iPad4,2", "iPad4,3":            return "iPad Air 1"
        case "iPad5,3", "iPad5,4":                       return "iPad Air 2"
        case "iPad2,5", "iPad2,6", "iPad2,7":            return "iPad Mini 1"
        case "iPad4,4", "iPad4,5", "iPad4,6":            return "iPad Mini 2"
        case "iPad4,7", "iPad4,8", "iPad4,9":            return "iPad Mini 3"
        case "iPad5,1", "iPad5,2":                       return "iPad Mini 4"
        case "iPad6,3", "iPad6,4":                       return "iPad Pro 9.7 Inch"
        case "iPad6,7", "iPad6,8":                       return "iPad Pro 12.9 Inch"
        case "iPad7,1", "iPad7,2":                       return "iPad Pro 12.9 Inch 2.Generation"
        case "iPad7,3", "iPad7,4":                       return "iPad Pro 10.5 Inch"

        case "iPhone3,1", "iPhone3,2", "iPhone3,3":      return "iPhone 4"
        case "iPhone4,1":                                return "iPhone 4s"
        case "iPhone5,1", "iPhone5,2":                   return "iPhone 5"
        case "iPhone5,3", "iPhone5,4":                   return "iPhone 5c"
        case "iPhone6,1", "iPhone6,2":                   return "iPhone 5s"
        case "iPhone7,2":                                return "iPhone 6"
        case "iPhone7,1":                                return "iPhone 6 Plus"
        case "iPhone8,1":                                return "iPhone 6s"
        case "iPhone8,2":                                return "iPhone 6s Plus"
        case "iPhone9,1", "iPhone9,3":                   return "iPhone 7"
        case "iPhone9,2", "iPhone9,4":                   return "iPhone 7 Plus"
        case "iPhone8,4":                                return "iPhone SE"
        case "iPhone10,1", "iPhone10,4":                 return "iPhone 8"
        case "iPhone10,2", "iPhone10,5":                 return "iPhone 8 Plus"
        case "iPhone10,3", "iPhone10,6":                 return "iPhone X"
        case "iPhone11,2":                               return "iPhone XS"
        case "iPhone11,4", "iPhone11,6":                 return "iPhone XS Max"
        case "iPhone11,8":                               return "iPhone XR"

        case "AppleTV5,3":                               return "Apple TV"
        case "AppleTV6,2":                               return "Apple TV 4K"
        case "AudioAccessory1,1":                        return "HomePod"
        case "i386", "x86_64":                           return "Simulator"
        default:                                         return identifier
        }
    }

    var type: DeviceType {
        switch self.modelName {
        case "iPod Touch 1":                    return .iPod1
        case "iPod Touch 2":                    return .iPod2
        case "iPod Touch 3":                    return .iPod3
        case "iPod Touch 4":                    return .iPod4
        case "iPod Touch 5":                    return .iPod5
        case "iPod Touch 6":                    return .iPod5

        case "iPad 2":                          return .iPad2
        case "iPad 3":                          return .iPad3
        case "iPad 4":                          return .iPad4
        case "iPad 5":                          return .iPad5
        case "iPad Air 1":                      return .iPadAir1
        case "iPad Air 2":                      return .iPadAir2
        case "iPad Mini 1":                     return .iPadMini1
        case "iPad Mini 2":                     return .iPadMini2
        case "iPad Mini 3":                     return .iPadMini3
        case "iPad Mini 4":                     return .iPadMini4
        case "iPad Pro 9.7 Inch":               return .iPadPro9_7
        case "iPad Pro 12.9 Inch":              return .iPadPro12_9
        case "iPad Pro 12.9 Inch 2.Generation": return .iPadPro12_9
        case "iPad Pro 10.5 Inch":              return .iPadPro10_5

        case "iPhone 4":                        return .iPhone4
        case "iPhone 4s":                       return .iPhone4S
        case "iPhone 5":                        return .iPhone5
        case "iPhone 5c":                       return .iPhone5C
        case "iPhone 5s":                       return .iPhone5S
        case "iPhone 6":                        return .iPhone6
        case "iPhone 6 Plus":                   return .iPhone6plus
        case "iPhone 6s":                       return .iPhone6S
        case "iPhone 6s Plus":                  return .iPhone6Splus
        case "iPhone 7":                        return .iPhone7
        case "iPhone 7 Plus":                   return .iPhone7plus
        case "iPhone SE":                       return .iPhoneSE
        case "iPhone 8":                        return .iPhone8
        case "iPhone 8 Plus":                   return .iPhone8plus
        case "iPhone X":                        return .iPhoneX
        case "iPhone XS":                       return .iPhoneXS
        case "iPhone XS Max":                   return .iPhoneXSMax
        case "iPhone XR":                       return .iPhoneXR

        case "Apple TV":                        return .appleTV
        case "Apple TV 4K":                     return .appleTV4K
        case "HomePod":                         return .homePod
        case "Simulator":                       return .simulator

        default:                                return .unrecognized
        }
    }
}

使用示例

if UIDevice.current.type == .iPhoneX {
     // iPhoneX
} else {
     // other
}
  1. 判断是否为流海屏
// 第一种
let isiphoneX = UIApplication.shared.statusBarFrame.height >= 44
// 第二种
var isiphoneX: Bool {
    if #available(iOS 11, *) {
        if let w = UIApplication.shared.delegate?.window,
          let window = w, window.safeAreaInsets.left > 0 || window.safeAreaInsets.bottom > 0 {
            return true
        }
    }
    return false
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,099评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,473评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,229评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,570评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,427评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,335评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,737评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,392评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,693评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,730评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,512评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,349评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,750评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,017评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,290评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,706评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,904评论 2 335

推荐阅读更多精彩内容

  • 我一遍又一遍地活着 活成一堆饵料 投饲于人间 这庞大的鱼池 一尾尾摇头摆尾的鱼啊 你们也不过是自己的赝品
    逸古逸尘阅读 210评论 1 2
  • 哈哈
    溪子星沿阅读 268评论 0 0
  • 这是昨天的,忘记写了 两个小时的公交车程去父母那里 给小弟买了本书 他骑小黄车去校门 看了两集电视剧 早上的习惯保...
    良辰美LiangChen阅读 216评论 0 0