Last active
July 16, 2025 16:42
-
-
Save ericdke/ed2d8bd3d127c25bcc6b to your computer and use it in GitHub Desktop.
Swift: get the Mac UUID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
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
Hi! I refined this a little bit more https://gist.github.com/vadimpiven/3373bb2592d59560b5d698ba1e2ed7e4