Skip to content

Instantly share code, notes, and snippets.

@ericdke
Last active October 31, 2023 05:04
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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

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