How do I get device id in Apple Cocoa application with swift 4.2?
Asked
Active
Viewed 1,680 times
2
-
1What does *device id* mean in terms of macOS? – vadian Mar 05 '19 at 11:20
-
Unique string id of each device – Mustafa Sevinç Mar 05 '19 at 11:22
-
iOS can also get UIDevice.current.identifierForVendor! .uuidString but this is not happening in MacOS. What can I do for MacOS app – Mustafa Sevinç Mar 05 '19 at 11:30
1 Answers
6
You can get the Hardware UUID with IOKit
func hardwareUUID() -> String?
{
let matchingDict = IOServiceMatching("IOPlatformExpertDevice")
let platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, matchingDict)
defer{ IOObjectRelease(platformExpert) }
guard platformExpert != 0 else { return nil }
return IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformUUIDKey as CFString, kCFAllocatorDefault, 0).takeRetainedValue() as? String
}

vadian
- 274,689
- 30
- 353
- 361