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

原文链接:https://stackoverflow.com/questions/26028918/how-to-determine-the-current-iphone-device-model/26962452#26962452 下面是我整理过后写成的扩展,可直接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 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 "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 "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
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏一个番茄说

函数响应式编程框架RxSwift 学习——Subject

简单的比喻下,Observable像是一个水管,会源源不断的有水冒出来。Subject就像一个水龙头,它可以套在水管上,接受Observable上面的事件。但是...

682
来自专栏一个会写诗的程序员的博客

BootstrapTable (前后端分页,表格 ajax 返回数据回调处理) 配置参数全说明

4674
来自专栏Micro_awake web

Vue学习4:class与style绑定

说明:有些部分我只是相当于做一个学习笔记,加强记忆之用。所以可能阅读性不是那么强。如果有参考我这类博客的人,那么请见谅。

820
来自专栏量子位

FPGA开发板剁手,学生狗省钱大法丨吐血资源

博主Joel Williams在他的主页中分享了一篇购买便宜的FPGA开发板的攻略,量子位编译本文。

520
来自专栏蜉蝣禅修之道

iOS开发之CFHttpMessageRef的那些坑

1876
来自专栏宋凯伦的技术小栈

【从业余项目中学习1】C# 实现XML存储用户名密码(MD5加密)

  最近在写一个C#的项目,用户需求是实现Winform的多文档界面与Matlab算法程序之间的交互。做了一段时间发现,这既能利用业余时间,实战中也可学习一些技...

21410
来自专栏学海无涯

iOS开发之XLForm的使用

在iOS开发中,开发"表单"界面,字段稍微多一点的一般都用UITableView来做,而XLForm就是这样一个框架,它是创建动态表格视图最牛逼的iOS库, 用...

3768
来自专栏机器人网

六中常用串口电路图大全

六中常用串口图设计。如果你是Win95/98/Me操作系统的话那第1和第2两种是最合适的,制作简单。如果是Win2000/XP操作系统的话第3种最合适,当然如果...

2505
来自专栏计算机编程

SNS项目笔记<六>--手势Gestures

这里直接贴上angular源码地址:angular源码之hammer_gestures <这里方便它更新后的修改> 这里又贴上该地址的源码以便说明:

1071
来自专栏张高兴的博客

张高兴的 UWP 开发笔记:汉堡菜单进阶

3586

扫码关注云+社区