9

I want to identify my mac system uniquely via code. I find the Hardware UUID in my About this Mac. So how to programmatically access the unique uuid from MAc OS X.

Kindly provide me if there are any alternative suggestion for my problem.

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Kakey
  • 3,936
  • 5
  • 29
  • 45

2 Answers2

21

So, if you don't care about the new AppStore rules etc... here you go:

- (NSString *)getSystemUUID {
    io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault,IOServiceMatching("IOPlatformExpertDevice"));
    if (!platformExpert)
        return nil;

    CFTypeRef serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert,CFSTR(kIOPlatformUUIDKey),kCFAllocatorDefault, 0);
    IOObjectRelease(platformExpert);
    if (!serialNumberAsCFString)
        return nil;

    return (__bridge NSString *)(serialNumberAsCFString);;
}

Please Note:

  • You need to add IOKit.framework to your project in order for this to work.
  • This code is ARC compliant;
  • This code is safe and it will return a nil NSString if something goes wrong;
  • Apple does not guarantee that all future systems will have a software-readable serial number.
  • Developers should not make any assumptions about the format of the serial number such as its length or what characters it may contain.
TCB13
  • 3,067
  • 2
  • 39
  • 68
  • Should `IOObjectRelease(platformExpert);` be also called within the `!serialNumberAsCFString` block? Reading through the code only, this seems needed. – Dj S Mar 27 '14 at 03:31
  • 2
    What if we care about the new AppStore rules? – tofutim Apr 23 '14 at 08:45
  • @tofutim I guess for now this can work on the AppStore, but in the future Apple might implement a Advertising Identifier scheme similar to what they already have at iOS. – TCB13 Apr 23 '14 at 12:07
  • Is this a good way to identify whether the user has a '*pro*' or '*paid*' account or can this be easily hacked? – maxisme Oct 31 '15 at 00:02
  • @TCB13 I believe the final `__bridge` needs to be `__bridge_transfer`. As written the string will leak. – Oz Solomon Nov 09 '15 at 22:17
3

From here: https://stackoverflow.com/a/2754563/610351

void get_platform_uuid(char * buf, int bufSize) {
    io_registry_entry_t ioRegistryRoot = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/");
    CFStringRef uuidCf = (CFStringRef) IORegistryEntryCreateCFProperty(ioRegistryRoot, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0);
    IOObjectRelease(ioRegistryRoot);
    CFStringGetCString(uuidCf, buf, bufSize, kCFStringEncodingMacRoman);
    CFRelease(uuidCf);    
}

You can replace the CFStringGetCString with a simple conversion to NSString*.

Community
  • 1
  • 1
Jaffa
  • 12,442
  • 4
  • 49
  • 101