Skip to content

Instantly share code, notes, and snippets.

@ericdke
Last active July 16, 2025 16:42
Show Gist options
  • Save ericdke/ed2d8bd3d127c25bcc6b to your computer and use it in GitHub Desktop.
Save ericdke/ed2d8bd3d127c25bcc6b 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, kCFAllocatorDefault, 0)
IOObjectRelease(platformExpert)
let ser: CFTypeRef = serialNumberAsCFString.takeUnretainedValue()
if let result = ser as? String {
return result
}
return nil
}
if let uuid = getSystemUUID() {
print(uuid)
}
@SLboat
Copy link

SLboat commented Jun 26, 2016

thank you!

@vincedev
Copy link

if let result = ser as? String {
    return result
}
return nil

you could simply : return ser as? String

@Eaffy
Copy link

Eaffy commented May 19, 2020

thank you!

add one

@vadimpiven
Copy link

@noah-nuebling
Copy link

noah-nuebling commented Jul 16, 2025

Thank you for sharing. I think this should be .takeRetainedValue() since you have ownership of serialNumberAsCFString. (See Create Rule)

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