Swiftでデバイスの種類を取得 （iPhone7,iPad Pro対応）

#使い方

デバイスの種類以外にも
見た通り色々取れます。


```swift

let device = Device()
print(device. modelType)//iPhone5とか

```

#実装

```swift

//==========================================================================
//Device
import UIKit

enum iOSModelType: String{
    case iPodTouch5
    case iPodTouch6
    case iPhone3
    case iPhone4
    case iPhone4s
    case iPhone5
    case iPhone5c
    case iPhone5s
    case iPhone6
    case iPhone6Plus
    case iPhone6s
    case iPhone6sPlus
    case iPhoneSE
    case iPhone7
    case iPhone7Plus
    case iPad2
    case iPad3
    case iPad4
    case iPadAir
    case iPadAir2
    case iPadMini
    case iPadMini2
    case iPadMini3
    case iPadMini4
    case iPadPro97
    case iPadPro129
    case simulator
    case unknowniPod
    case unknowniPhone
    case unknowniPad
    case unknown
}

class Device:NSObject{
    //==========================================================================
    //public properties
    var size = UIScreen.mainScreen().bounds.size
    var frame = UIScreen.mainScreen().bounds
    var width = UIScreen.mainScreen().bounds.size.width
    var height = UIScreen.mainScreen().bounds.size.height
    var orientation = UIDevice().orientation
    var statusBarHeight = UIApplication.sharedApplication().statusBarFrame.height
    var modelType: iOSModelType!
    var is_iPod = false
    var is_iPhone = false
    var is_iPad = false
    //====================================
    //init
    override init() {
        super.init()
        modelType = getModelType()
        setUpModelDeviceType()
    }
    //==========================================================================
    //private methods
    private func getModelType()->iOSModelType{
        var systemInfo = utsname()
        uname(&systemInfo)
        
        let mirror = Mirror(reflecting: systemInfo.machine)
        var identifier = ""
        
        for child in mirror.children {
            if let value = child.value as? Int8{
                if value != 0{
                    identifier.append(UnicodeScalar(UInt8(value)))
                }
            }
        }
        
        if let myDevice = DeviceList[identifier]{
            return myDevice
        }else{
            if identifier.containsString("iPod"){
                return .unknowniPod
            }else if identifier.containsString("iPhone"){
                return .unknowniPhone
            }else if identifier.containsString("iPad"){
                return .unknowniPad
            }else{
                return .unknown
            }
        }
    }
    private func setUpModelDeviceType(){
        if modelType.rawValue.containsString("iPhone") {
            is_iPhone = true
        }else if modelType.rawValue.containsString("iPad"){
            is_iPad = true
        }else if modelType.rawValue.containsString("iPod"){
            is_iPod = true
        }
    }
    //===================================================
    //Data
    private let DeviceList:[String:iOSModelType] = [
        "iPod5,1": .iPodTouch5,
        "iPod7,1": .iPodTouch6,
        "iPhone3,1": .iPhone4,
        "iPhone3,2": .iPhone4,
        "iPhone3,3": .iPhone4,
        "iPhone4,1": .iPhone4s,
        "iPhone5,1": .iPhone5,
        "iPhone5,2": .iPhone5,
        "iPhone5,3": .iPhone5c,
        "iPhone5,4": .iPhone5c,
        "iPhone6,1": .iPhone5s,
        "iPhone6,2": .iPhone5s,
        "iPhone7,2": .iPhone6,
        "iPhone7,1": .iPhone6Plus,
        "iPhone8,2": .iPhone6s,
        "iPhone8,1": .iPhone6sPlus,
        "iPhone8,4": .iPhoneSE,
        "iPhone9,1": .iPhone7,
        "iPhone9,2": .iPhone7Plus,
        "iPad2,1": .iPad2,
        "iPad2,2": .iPad2,
        "iPad2,3": .iPad2,
        "iPad2,4": .iPad2,
        "iPad2,5": .iPadMini,
        "iPad2,6": .iPadMini,
        "iPad2,7": .iPadMini,
        "iPad3,1": .iPad3,
        "iPad3,2": .iPad3,
        "iPad3,3": .iPad3,
        "iPad3,4": .iPad4,
        "iPad3,5": .iPad4,
        "iPad3,6": .iPad4,
        "iPad4,1": .iPadAir,
        "iPad4,2": .iPadAir,
        "iPad4,3": .iPadAir,
        "iPad4,4": .iPadMini2,
        "iPad4,5": .iPadMini2,
        "iPad4,6": .iPadMini2,
        "iPad4,7": .iPadMini3,
        "iPad4,8": .iPadMini3,
        "iPad4,9": .iPadMini3,
        "iPad5,1": .iPadMini4,
        "iPad5,2": .iPadMini4,
        "iPad5,3": .iPadAir2,
        "iPad5,4": .iPadAir2,
        "iPad6,3": .iPadPro97,
        "iPad6,4": .iPadPro97,
        "iPad6,7": .iPadPro129,
        "iPad6,8": .iPadPro129,
        "x86_64": .simulator,
        "i386": .simulator
    ]
}

```

#参考

http://qiita.com/ryokosuge/items/9d954f54ffc6692c89c4