Possible Duplicate:
Unique Identifier of a Mac?
On iOS, retrieving a unique and anonymous string for the current device is开发者_如何学JAVA 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?
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);
}
}
}
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] );
}
精彩评论