Skip to content

Instantly share code, notes, and snippets.

@eryshkov
Last active February 6, 2019 09:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eryshkov/7ddcb89e00a2dc00a951426c80a99133 to your computer and use it in GitHub Desktop.
Save eryshkov/7ddcb89e00a2dc00a951426c80a99133 to your computer and use it in GitHub Desktop.
[UUID and Serial for mac]
//: Playground - noun: a place where people can play
import Cocoa
func getSerial() -> String? {//Получить серийный номер mac-компьютера
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
}
func getSystemUUID() -> String? {//Получить SystemUUID для устройства Apple
let dev = IOServiceMatching("IOPlatformExpertDevice")
let platformExpert: io_service_t = IOServiceGetMatchingService(kIOMasterPortDefault, dev)
let serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformUUIDKey as CFString, kCFAllocatorDefault, 0)
IOObjectRelease(platformExpert)
let ser: CFTypeRef = serialNumberAsCFString!.takeUnretainedValue()
if let result = ser as? String {
return result
}
return nil
}
print("your mac serial is \(getSerial() ?? "not found")")
print("Your system UUID is \(getSystemUUID() ?? "not found")")
@uchqur
Copy link

uchqur commented Feb 6, 2019

It's working fine. Thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment