UIDevice expand, access to current device name Use: the let deveiceModelName = UIDevice deveiceModelName
code
mport Foundation
import SystemConfiguration
import UIKit
public extension UIDevice {
static let deveiceModelName: 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)))
}
func mapToDevice(identifier: String) -> String {
#if os(iOS)
switch identifier {
case "IPod5, 1": return "iPod Touch 5"
case "IPod7, 1": return "iPod Touch 6"
case "However, 1"."However, 2"."However, 3": return "iPhone 4"
case "The iphone 4, 1": return "iPhone 4s"
case "The iphone 5, 1"."The iphone 5, 2": return "iPhone 5"
case "The iphone 5, 3"."The iphone 5, 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 "And 1"."And 2"."And 3"."And 4":return "iPad 2"
case "IPad3, 1"."IPad3, 2"."IPad3, 3": return "iPad 3"
case "IPad3, 4"."Ipad 3, 5"."IPad3, 6": return "iPad 4"
case "IPad4, 1"."IPad4, 2"."IPad4, 3": return "iPad Air"
case "The lofty, 3"."Lofty, 4": return "iPad Air 2"
case "IPad6, 11"."IPad6, 12": return "iPad 5"
case "IPad7, 5"."IPad7, 6": return "iPad 6"
case "And 5"."And 6"."And 7": return "iPad Mini"
case "IPad4, 4"."IPad4, 5"."IPad4, 6": return "iPad Mini 2"
case "IPad4, 7"."IPad4, 8"."IPad4, 9": return "iPad Mini 3"
case "Lofty, 1"."The lofty, 2": return "iPad Mini 4"
case "IPad6, 3"."IPad6, 4": return "The Pro (9.7 inch)"
case "IPad6, 7"."IPad6, 8": return "The Pro (12.9 inch)"
case "IPad7, 1"."IPad7, 2": return IPad Pro (12.9-inch) (2nd Generation)
case "IPad7, 3"."IPad7, 4": return "The Pro (10.5 inch)"
case "IPad8, 1"."IPad8, 2"."IPad8, 3"."IPad8, 4":return "iPad Pro (11-inch)"
case "IPad8, 5"."IPad8, 6"."IPad8, 7"."IPad8, 8":return "iPad Pro (12.9-inch) (3rd generation)"
case "AppleTV5, 3": return "Apple TV"
case "AppleTV6, 2": return "Apple TV 4K"
case "AudioAccessory1, 1": return "HomePod"
case "i386"."x86_64": return "Simulator \(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "iOS"))"
default: return identifier
}
#elseif os(tvOS)
switch identifier {
case "AppleTV5, 3": return "Apple TV 4"
case "AppleTV6, 2": return "Apple TV 4K"
case "i386"."x86_64": return "Simulator \(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "tvOS"))"
default: return identifier
}
#endif
}
return mapToDevice(identifier: identifier)
}()
}
Copy the code
Demo: Github