techno
techno

Reputation: 6508

Uniquely Identifying an OSX Machine

I want to Identify a Mac based on Hardware ID.I have seen this

https://developer.apple.com/library/content/technotes/tn1103/_index.html

It gives mainly 2 approaches

1 -Using S/N: But it seems this might not be supported in future Machines

2 -Using MAC ID: I don't know if this is reliable

Could you please suggest the best way and provide me some sample code in SWIFT to start with.Almost all examples are in Objective C. I'm new to SWIFT as well.

Upvotes: 2

Views: 1274

Answers (1)

laike9m
laike9m

Reputation: 19368

I think serial number is your best shot. I tested the code on M1 and M3 Macs, both work correctly without issue, so likely it will stay for future versions too:

func getSerialNumber() -> String? {
  let platformExpert = IOServiceGetMatchingService(
    kIOMainPortDefault, IOServiceMatching("IOPlatformExpertDevice"))

  guard platformExpert > 0 else {
    return nil
  }

  guard let serialNumber = (IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformSerialNumberKey as CFString, kCFAllocatorDefault, 0).takeUnretainedValue() as? String)?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) else {
    return nil
  }

  IOObjectRelease(platformExpert)

  return serialNumber
}

from https://gist.github.com/leogdion/77f6143ecf793e1ba381917d4b3b286c

Upvotes: 0

Related Questions