Skip to content

Instantly share code, notes, and snippets.

@donly
Forked from ericdke/getMacUUID.swift
Last active April 7, 2022 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donly/072d8e6beabaf4421c0248a34ddb7c29 to your computer and use it in GitHub Desktop.
Save donly/072d8e6beabaf4421c0248a34ddb7c29 to your computer and use it in GitHub Desktop.
Swift: get the Mac UUID
func getSystemUUID() -> String? {
let dev = IOServiceMatching("IOPlatformExpertDevice")
let platformExpert: io_service_t = IOServiceGetMatchingService(kIOMasterPortDefault, dev)
let serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformUUIDKey as CFString, kCFAllocatorDefault, 0)
IOObjectRelease(platformExpert)
guard let ser: CFTypeRef = serialNumberAsCFString?.takeUnretainedValue() else { return nil }
if let result = ser as? String {
return result
}
return nil
}
if let uuid = getSystemUUID() {
print(uuid)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment