3

Possible Duplicate:
Unique Identifier of a Mac?

On iOS, retrieving a unique and anonymous string for the current device is fairly easy ([[UIDevice currentDevice] uniqueIdentifier]). And I'm not referring here to the computer's serial number but at a hash made out of different components characteristics/sn, like the iOS udid's.

Is there something similar on Mac OS X side, and how to access it?

Community
  • 1
  • 1
Remy Vanherweghem
  • 3,724
  • 1
  • 27
  • 43

2 Answers2

1

See Tech Note 1103:

#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>

// Returns the serial number as a CFString. 
// It is the caller's responsibility to release the returned CFString when done with it.
void CopySerialNumber(CFStringRef *serialNumber)
{
    if (serialNumber != NULL) {
        *serialNumber = NULL;

        io_service_t    platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault,
                                                                         IOServiceMatching("IOPlatformExpertDevice"));

        if (platformExpert) {
            CFTypeRef serialNumberAsCFString = 
                IORegistryEntryCreateCFProperty(platformExpert,
                                                CFSTR(kIOPlatformSerialNumberKey),
                                                kCFAllocatorDefault, 0);
            if (serialNumberAsCFString) {
                *serialNumber = serialNumberAsCFString;
            }

            IOObjectRelease(platformExpert);
        }
    }
}
sbooth
  • 15,900
  • 2
  • 51
  • 76
1

Don't use the computer serial number— it's only valid on initial factory installations. If your motherboard gets replaced at any point, you'll no longer have a serial number, since it wasn't setup to have one as part of a full machine at the factory.

Instead, you should use the hardware ethernet ID, specifically the one for device 'en0'. The following (quite similar) code will give you that:

//
//  MACAddress.m
//  XPPublisherCore
//
//  Created by Jim Dovey on 11-01-30.
//  Copyright 2011 XPlatform Inc. All rights reserved.
//

#import "MACAddress.h"
#import <IOKit/IOKitLib.h>

NSData * GetMACAddress( void )
{
    kern_return_t           kr          = KERN_SUCCESS;
    CFMutableDictionaryRef  matching    = NULL;
    io_iterator_t           iterator    = IO_OBJECT_NULL;
    io_object_t             service     = IO_OBJECT_NULL;
    CFDataRef               result      = NULL;

    matching = IOBSDNameMatching( kIOMasterPortDefault, 0, "en0" );
    if ( matching == NULL )
    {
        fprintf( stderr, "IOBSDNameMatching() returned empty dictionary\n" );
        return ( NULL );
    }

    kr = IOServiceGetMatchingServices( kIOMasterPortDefault, matching, &iterator );
    if ( kr != KERN_SUCCESS )
    {
        fprintf( stderr, "IOServiceGetMatchingServices() returned %d\n", kr );
        return ( NULL );
    }

    while ( (service = IOIteratorNext(iterator)) != IO_OBJECT_NULL )
    {
        io_object_t parent = IO_OBJECT_NULL;

        kr = IORegistryEntryGetParentEntry( service, kIOServicePlane, &parent );
        if ( kr == KERN_SUCCESS )
        {
            if ( result != NULL )
                CFRelease( result );

            result = IORegistryEntryCreateCFProperty( parent, CFSTR("IOMACAddress"), kCFAllocatorDefault, 0 );
            IOObjectRelease( parent );
        }
        else
        {
            fprintf( stderr, "IORegistryGetParentEntry returned %d\n", kr );
        }

        IOObjectRelease( service );
    }

    return ( (NSData *)NSMakeCollectable(result) );
}

NSString * GetMACAddressDisplayString( void )
{
    NSData * macData = GetMACAddress();
    if ( [macData length] == 0 )
        return ( nil );

    const UInt8 *bytes = [macData bytes];

    NSMutableString * result = [NSMutableString string];
    for ( NSUInteger i = 0; i < [macData length]; i++ )
    {
        if ( [result length] != 0 )
            [result appendFormat: @":%02hhx", bytes[i]];
        else
            [result appendFormat: @"%02hhx", bytes[i]];
    }

    return ( [[result copy] autorelease] );
}
Jim Dovey
  • 10,998
  • 2
  • 31
  • 38
  • Why would the MAC address not change if the motherboard was replaced? – sbooth Aug 17 '11 at 01:24
  • I've had a MacBook Pro motherboard replaced, with the Ethernet MAC address changing, but the serial number preserved. The Wi-Fi MAC didn't change, since it's on a separate board. – user57368 Aug 17 '11 at 04:10
  • The MAC address would change, yes, but the serial number can disappear completely. I used to use the serial as a UDID, but frequently many universities contacted us to say that after a machine was repaired its serial number was cleared, so our software broke because we got a nil UDID back. Then we switched to MAC & everything was fine— admins were happy with the UDID changing, so long as the software still worked at all. – Jim Dovey Aug 17 '11 at 15:29
  • it was a big problem on cheap PC motherboards as well, the oem was supposed to set it but didn't, iirc they all came with the same bios ID equivalent of 1234 – Conrad Jones Dec 16 '19 at 03:15