-
-
Save leogdion/77f6143ecf793e1ba381917d4b3b286c to your computer and use it in GitHub Desktop.
var serialNumber: String? { | |
let platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice") ) | |
guard platformExpert > 0 else { | |
return nil | |
} | |
guard let serialNumber = (IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformSerialNumberKey as CFString, kCFAllocatorDefault, 0).takeUnretainedValue() as? String)?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) else { | |
return nil | |
} | |
IOObjectRelease(platformExpert) | |
return serialNumber | |
} |
Anyone have any idea for M1 chips or newer macs? Don't know if it works for newer macs
This is what I just was able to figure out to get working for me, granted I am brand new to figuring out Swift but I was able to get a serial number on a M2 chip when called with text(serialNumber).
`
var serialNumber: String {
let platformExpert = IOServiceGetMatchingService(kIOMainPortDefault, IOServiceMatching("IOPlatformExpertDevice") )
guard platformExpert > 0 else {
return ""
}
guard let serialNumber = (IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformSerialNumberKey as CFString, kCFAllocatorDefault, 0).takeUnretainedValue() as? String)?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) else {
return ""
}
IOObjectRelease(platformExpert)
return serialNumber
}
`
Anyone have any idea for M1 chips or newer macs? Don't know if it works for newer macs