Skip to content

Instantly share code, notes, and snippets.

@ericdke
Last active July 16, 2025 16:42
Show Gist options
  • Select an option

  • Save ericdke/ed2d8bd3d127c25bcc6b to your computer and use it in GitHub Desktop.

Select an option

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

SLboat commented Jun 26, 2016

Copy link
Copy Markdown

thank you!

@vincedev

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

you could simply : return ser as? String

@Eaffy

Eaffy commented May 19, 2020

Copy link
Copy Markdown

thank you!

add one

@vadimpiven

Copy link
Copy Markdown

@noah-nuebling

noah-nuebling commented Jul 16, 2025

Copy link
Copy Markdown

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