在Swift的iOS7或更高版本上完全正常工作
func DeviceName()-> String {
var myDeviceName : String = String()
var systemInfo = [UInt8](count: sizeof(utsname), repeatedValue: 0)
let model = systemInfo.withUnsafeMutableBufferPointer { (inout body: UnsafeMutableBufferPointer<UInt8>) -> String? in
if uname(UnsafeMutablePointer(body.baseAddress)) != 0 {
return nil
}
return String.fromCString(UnsafePointer(body.baseAddress.advancedBy(Int(_SYS_NAMELEN * 4))))
}
let deviceNamesByCode : [String: String] = ["iPod1,1":"iPod Touch 1",
"iPod2,1":"iPod Touch 2",
"iPod3,1":"iPod Touch 3",
"iPod4,1":"iPod Touch 4",
"iPod5,1":"iPod Touch 5",
"iPod7,1":"iPod Touch 6",
"iPhone1,1":"iPhone",
"iPhone1,2":"iPhone ",
"iPhone2,1":"iPhone ",
"iPhone3,1":"iPhone 4",
"iPhone3,2":"iPhone 4",
"iPhone3,3":"iPhone 4",
"iPhone4,1":"iPhone 4s",
"iPhone5,1":"iPhone 5",
"iPhone5,2":"iPhone 5",
"iPhone5,3":"iPhone 5c",
"iPhone5,4":"iPhone 5c",
"iPhone6,1":"iPhone 5s",
"iPhone6,2":"iPhone 5s",
"iPhone7,2":"iPhone 6",
"iPhone7,1":"iPhone 6 Plus",
"iPhone8,1":"iPhone 6s",
"iPhone8,2":"iPhone 6s Plus",
"iPhone8,4":"iPhone SE",
"iPad2,1":"iPad 2",
"iPad2,2":"iPad 2",
"iPad2,3":"iPad 2",
"iPad2,4":"iPad 2",
"iPad3,1":"iPad 3",
"iPad3,2":"iPad 3",
"iPad3,3":"iPad 3",
"iPad3,4":"iPad 4",
"iPad3,5":"iPad 4",
"iPad3,6":"iPad 4",
"iPad4,1":"iPad Air",
"iPad4,2":"iPad Air",
"iPad4,3":"iPad Air",
"iPad5,3":"iPad Air 2",
"iPad5,4":"iPad Air 2",
"iPad2,5":"iPad Mini",
"iPad2,6":"iPad Mini",
"iPad2,7":"iPad Mini",
"iPad4,4":"iPad Mini 2",
"iPad4,5":"iPad Mini 2",
"iPad4,6":"iPad Mini 2",
"iPad4,7":"iPad Mini 3",
"iPad4,8":"iPad Mini 3",
"iPad4,9":"iPad Mini 3",
"iPad5,1":"iPad Mini 4",
"iPad5,2":"iPad Mini 4",
"iPad6,3":"iPad Pro",
"iPad6,4":"iPad Pro",
"iPad6,7":"iPad Pro",
"iPad6,8":"iPad Pro",
"AppleTV5,3":"Apple TV",
"i386":"Simulator",
"x86_64":"Simulator"
]
if model!.characters.count > 0 {
myDeviceName = deviceNamesByCode[model!]!
}else{
myDeviceName = UIDevice.currentDevice().model
}
print("myDeviceName==\(myDeviceName)")
return myDeviceName
}