views:

297

answers:

3

How do I generate a UUID that will uniquely identify a computer using Xcode / Objective-C? Windows has UuidCreateSequential(), what can I use on a Mac?

+2  A: 

Try this:

CFUUIDRef UUID = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef UUIDString = CFUUIDCreateString(kCFAllocatorDefault,UUID);
// UUIDString is the CFStringRef (== NSString *) that contains the UUID.

// Cleanup
CFRelease(UUID);
CFRelease(UUIDString);  
Diederik Hoogenboom
Will this be unique for a computer across the world?
Chetan
And is it Mac-specific, or will the Windows version (`UuidCreateSequential()`) give the same result?
Chetan
According to this: http://developer.apple.com/mac/library/documentation/CoreFoundation/Reference/CFUUIDRef/Reference/reference.html, the UUID is unique across time and space. I need it to be the same across time because I want to be able to use it at any time to uniquely identify a computer. How do I do this?
Chetan
You could use the MAC address. See GetPrimaryMACAddress sample code in XCode Developer Documentation.
Diederik Hoogenboom
A: 
[[NSProcessInfo processInfo] globallyUniqueString];
Graham Lee
globallyUniqueString is guarantied to be unique across the network (according to the docs). globallyUniqueString also doesn't guarantee the length or nature of the returned string, while UUID's have a prescribed format.
Diederik Hoogenboom
+1  A: 

You can read the system serial number or the hardware MAC addresses using IOKit.

Check out Apple Technical Note TN1103 ("Uniquely Identifying a Macintosh Computer") for sample code and more information.

colinm