Skip to content

Instantly share code, notes, and snippets.

@backslash-f
Last active December 9, 2022 14:26
Show Gist options
  • Save backslash-f/0e9b41a6a2a13f40625cdc4da77d7f78 to your computer and use it in GitHub Desktop.
Save backslash-f/0e9b41a6a2a13f40625cdc4da77d7f78 to your computer and use it in GitHub Desktop.
Get Apple device model / marketing Name
import UIKit
public enum ModelName: String {
case simulator
case iPod1, iPod2, iPod3, iPod4, iPod5
case iPad2, iPad3, iPad4, iPad5, iPad6
case iPadAir, iPadAir2, iPadAir3
case iPadMini, iPadMini2, iPadMini3, iPadMini4, iPadMini5
case iPadPro9_7, iPadPro10_5, iPadPro12_9, iPadPro2_12_9, iPadPro11, iPadPro3_12_9
case iPhone4, iPhone4S
case iPhone5, iPhone5C, iPhone5S
case iPhone6Plus, iPhone6, iPhone6S, iPhone6SPlus
case iPhoneSE
case iPhone7, iPhone7Plus
case iPhone8, iPhone8Plus
case iPhoneX, iPhoneXS, iPhoneXSMax, iPhoneXR
case AppleTV, AppleTV_4K
case unknown
}
extension ModelName {
init(string: String) {
switch string {
case "i386", "x86_64":
self = .simulator
case "iPod1,1":
self = .iPod1
case "iPod2,1":
self = .iPod2
case "iPod3,1":
self = .iPod3
case "iPod4,1":
self = .iPod4
case "iPod5,1":
self = .iPod5
case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":
self = .iPad2
case "iPad3,1", "iPad3,2", "iPad3,3":
self = .iPad3
case "iPad3,4", "iPad3,5", "iPad3,6":
self = .iPad4
case "iPad6,11", "iPad6,12":
self = .iPad5
case "iPad7,5", "iPad7,6":
self = .iPad6
case "iPad4,1", "iPad4,2", "iPad4,3":
self = .iPadAir
case "iPad5,3", "iPad5,4":
self = .iPadAir2
case "iPad11,3", "iPad11,4":
self = .iPadAir3
case "iPad2,5", "iPad2,6", "iPad2,7":
self = .iPadMini
case "iPad4,4", "iPad4,5", "iPad4,6":
self = .iPadMini2
case "iPad4,7", "iPad4,8", "iPad4,9":
self = .iPadMini3
case "iPad5,1", "iPad5,2":
self = .iPadMini4
case "iPad11,1", "iPad11,2":
self = .iPadMini5
case "iPad6,3", "iPad6,4":
self = .iPadPro9_7
case "iPad7,3", "iPad7,4":
self = .iPadPro10_5
case "iPad6,7", "iPad6,8":
self = .iPadPro12_9
case "iPad7,1", "iPad7,2":
self = .iPadPro2_12_9
case "iPad8,1", "iPad8,2", "iPad8,3", "iPad8,4":
self = .iPadPro11
case "iPad8,5", "iPad8,6", "iPad8,7", "iPad8,8":
self = .iPadPro3_12_9
case "iPhone3,1", "iPhone3,2", "iPhone3,3":
self = .iPhone4
case "iPhone4,1":
self = .iPhone4S
case "iPhone5,1", "iPhone5,2":
self = .iPhone5
case "iPhone5,3", "iPhone5,4":
self = .iPhone5C
case "iPhone6,1", "iPhone6,2":
self = .iPhone5S
case "iPhone7,2":
self = .iPhone6
case "iPhone8,1":
self = .iPhone6S
case "iPhone7,1", "iPhone8,2":
self = .iPhone6Plus
case "iPhone8,4":
self = .iPhoneSE
case "iPhone9,1", "iPhone9,3":
self = .iPhone7
case "iPhone9,2", "iPhone9,4":
self = .iPhone7Plus
case "iPhone10,1", "iPhone10,4":
self = .iPhone8
case "iPhone10,2", "iPhone10,5":
self = .iPhone8Plus
case "iPhone10,3", "iPhone10,6":
self = .iPhoneX
case "iPhone11,2":
self = .iPhoneXS
case "iPhone11,4", "iPhone11,6":
self = .iPhoneXSMax
case "iPhone11,8":
self = .iPhoneXR
case "AppleTV5,3":
self = .AppleTV
case "AppleTV6,2":
self = .AppleTV_4K
default:
self = .unknown
}
}
}
public extension UIDevice {
/// Checks and returns the device's `ModelName`.
///
/// [source](https://stackoverflow.com/a/30075200/584548)
///
/// - Returns: The device's `ModelName`. For example: `.iPhoneXSMax` is returned for the raw `String` *iPhone11,6*.
/// The raw `String` is extracted from `uname(&sysinfo)`.
func modelName() -> ModelName {
var sysinfo = utsname()
uname(&sysinfo) // Get the name of the current system.
let modelDate = Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN))
guard let modelString = String(bytes: modelDate, encoding: .ascii),
let model = ModelName(rawValue: modelString.trimmingCharacters(in: .controlCharacters)) else {
return .unknown
}
return model
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment