Is there a method to generate a standard 128bit GUID (UUID) on the Mac?

UUIDs are handled in Core Foundation, by the CFUUID library. The function you are looking for is CFUUIDCreate.

FYI for further searches: these are most commonly known as UUIDs, the term GUID isn't used very often outside of the Microsoft world. You might have more luck with that search term.


Some code:

For a string UUID, the following class method should do the trick:

+(NSString*)UUIDString {
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}

if you really want the bytes (not the string):

+(CFUUIDBytes)UUIDBytes {
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFUUIDBytes bytes = CFUUIDGetUUIDBytes(theUUID);
    CFRelease(theUUID);
    return bytes;
}

where CFUUIDBytes is a struct of the UUID bytes.


At least on MacOSX 10.5.x you might use the command line tool "uuidgen" to get your string e.g.

$ uuidgen

054209C4-3873-4679-8104-3C18AE780512

there's also an option -hdr with this comand that conveniently generates it in header style

See man-page for further infos.