前言:在开发中需要统计用户信息,如设备的型号、iOS版本等,将其封装。
反馈请联系hertz@hertzwang.com,谢谢
使用
UIDevice.current.deviceModel
UIDevice.current.deviceFamily
// ...
代码:
Swift
extension UIDevice {
//设备码 : iPhone7,2, iPhone6,1, ...
public var deviceCode: String {
var sysInfo: [CChar] = Array.init(repeating: 0, count: MemoryLayout.size(ofValue: utsname()))
let code = sysInfo.withUnsafeMutableBufferPointer { ( ptr: inout UnsafeMutableBufferPointer<CChar>) -> String in
guard let pointer = ptr.baseAddress?.withMemoryRebound(to: utsname.self, capacity: 1, { ptr in
return ptr
}) else {
return ""
}
uname(pointer)
let machinePtr = ptr.baseAddress?.advanced(by: Int(_SYS_NAMELEN * 4))
return String.init(cString: machinePtr!)
}
return code
}
// 设备是什么 iPhone,iPad, ...
public var deviceFamily: String {
return UIDevice.current.model
}
// 设备型号 参考:https://www.theiphonewiki.com/wiki/Models
public var deviceModel: String {
var model : String
let deviceCode = UIDevice().deviceCode
switch deviceCode {
case "iPod1,1": model = "iPod Touch 1G"
case "iPod2,1": model = "iPod Touch 2G"
case "iPod3,1": model = "iPod Touch 3G"
case "iPod4,1": model = "iPod Touch 4G"
case "iPod5,1": model = "iPod Touch 5G"
case "iPod7,1": model = "iPod Touch 6G"
case "iPhone1,1": model = "iPhone 2G"
case "iPhone1,2": model = "iPhone 3G"
case "iPhone2,1": model = "iPhone 3GS"
case "iPhone3,1", "iPhone3,2", "iPhone3,3": model = "iPhone 4"
case "iPhone3,1", "iPhone3,2", "iPhone3,3": model = "iPhone 4"
case "iPhone4,1": model = "iPhone 4S"
case "iPhone5,1", "iPhone5,2": model = "iPhone 5"
case "iPhone5,3", "iPhone5,4": model = "iPhone 5C"
case "iPhone6,1", "iPhone6,2": model = "iPhone 5S"
case "iPhone7,2": model = "iPhone 6"
case "iPhone7,1": model = "iPhone 6 Plus"
case "iPhone8,1": model = "iPhone 6S"
case "iPhone8,2": model = "iPhone 6S Plus"
case "iPhone8,4": model = "iPhone SE"
case "iPhone9,1", "iPhone9,3": model = "iPhone 7"
case "iPhone9,2", "iPhone9,4": model = "iPhone 7 Plus"
case "iPhone10,1", "iPhone10,4": model = "iPhone 8"
case "iPhone10,2", "iPhone10,5": model = "iPhone 8 Plus"
case "iPhone10,3", "iPhone10,6": model = "iPhone X"
case "iPad1,1": model = "iPad 1"
case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":model = "iPad 2"
case "iPad3,1", "iPad3,2", "iPad3,3": model = "iPad 3"
case "iPad3,4", "iPad3,5", "iPad3,6": model = "iPad 4"
case "iPad4,1", "iPad4,2", "iPad4,3": model = "iPad Air"
case "iPad5,1", "iPad5,3", "iPad5,4": model = "iPad Air 2"
case "iPad2,5", "iPad2,6", "iPad2,7": model = "iPad Mini"
case "iPad4,4", "iPad4,5", "iPad4,6": model = "iPad Mini 2"
case "iPad4,7", "iPad4,8", "iPad4,9": model = "iPad Mini 3"
case "iPad5,1", "iPad5,2": model = "iPad Mini 4"
case "iPad6,7", "iPad6,8": model = "iPad Pro"
case "i386", "x86_64": model = "Simulator"
default: model = deviceCode //If unkhnown
}
return model
}
// 获取设备型号方式2,设备型号不全!
// public var deviceModel2: String {
// let name = UnsafeMutablePointer<utsname>.allocate(capacity: 1)
// uname(name)
// let machine = withUnsafePointer(to: &name.pointee.machine, { (ptr) -> String? in
//
// let int8Ptr = unsafeBitCast(ptr, to: UnsafePointer<CChar>.self)
// return String.init(cString: int8Ptr)
// })
// name.deallocate(capacity: 1)
// if let deviceString = machine {
// switch deviceString {
// //iPhone
// case "iPhone1,1": return "iPhone 1G"
// case "iPhone1,2": return "iPhone 3G"
// case "iPhone2,1": return "iPhone 3GS"
// case "iPhone3,1", "iPhone3,2": 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,1": return "iPhone 6 Plus"
// case "iPhone7,2": return "iPhone 6"
// case "iPhone8,1": return "iPhone 6s"
// case "iPhone8,2": return "iPhone 6s Plus"
// case "iPhone8,4": return "iPhone SE"
// case "iPhone9,1": return "iPhone 7"
// case "iPhone9,2": return "iPhone 7 Plus"
// case "iPhone10,1": return "iPhone 8"
// case "iPhone10,4": return "iPhone 8"
// case "iPhone10,2": return "iPhone 8 Plus"
// case "iPhone10,5": return "iPhone 8 Plus"
// case "iPhone10,3": return "iPhone X"
// case "iPhone10,6": return "iPhone X"
//
// case "iPad3,1": return "iPad 3"
// case "iPad3,2": return "iPad 3"
// case "iPad3,3": return "iPad 3"
//
// case "iPad3,4": return "iPad 4"
// case "iPad3,5": return "iPad 4"
// case "iPad3,6": return "iPad 4"
// case "iPad4,1": return "iPad Air"
// case "iPad4,2": return "iPad Air"
// case "iPad4,3": return "iPad Air"
// default:
// return deviceString
// }
// } else {
// return ""
// }
// }
//IOS 版本号
public var deviceIOSVersion: String {
return UIDevice.current.systemVersion
}
//设备屏宽高
public var deviceScreenWidth: CGFloat {
let screenSize: CGRect = UIScreen.main.bounds
let width = screenSize.width;
return width
}
public var deviceScreenHeight: CGFloat {
let screenSize: CGRect = UIScreen.main.bounds
let height = screenSize.height;
return height
}
//小于5S 的设备
public var lessIphone5S : Bool {
switch UIDevice().deviceModel {
case "iPhone 4" , "iPhone 4S" , "iPhone 5" , "iPhone 5C" ,"iPhone 5S" :
return true
default : break
}
return false
}
//设备方向
public var deviceOrientationString: String {
var orientation : String
switch UIDevice.current.orientation{
case .portrait:
orientation="Portrait"
case .portraitUpsideDown:
orientation="Portrait Upside Down"
case .landscapeLeft:
orientation="Landscape Left"
case .landscapeRight:
orientation="Landscape Right"
case .faceUp:
orientation="Face Up"
case .faceDown:
orientation="Face Down"
default:
orientation="Unknown"
}
return orientation
}
// 设备方向 Landscape Portrait
public var isDevicePortrait: Bool {
return UIDevice.current.orientation.isPortrait
}
public var isDeviceLandscape: Bool {
return UIDevice.current.orientation.isLandscape
}
}