Last active
October 31, 2023 05:04
-
-
Save ericdke/ed2d8bd3d127c25bcc6b to your computer and use it in GitHub Desktop.
Swift: get the Mac UUID
This file contains 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!
add one
Hi! I refined this a little bit more https://gist.github.com/vadimpiven/3373bb2592d59560b5d698ba1e2ed7e4
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you could simply :
return ser as? String