Skip to content

Instantly share code, notes, and snippets.

@gonecoding
Created November 28, 2011 13:56
Show Gist options
  • Save gonecoding/1400481 to your computer and use it in GitHub Desktop.
Save gonecoding/1400481 to your computer and use it in GitHub Desktop.
Persistent Unique Identifier (GUID, UUID, UDID)
/*
UUIDs (Universally Unique Identifiers), also known as GUIDs (Globally Unique Identifiers)
or IIDs (Interface Identifiers), are 128-bit values guaranteed to be unique. A UUID is
made unique over both space and time by combining a value unique to the computer on which
it was generated—usually the Ethernet hardware address—and a value representing the number
of 100-nanosecond intervals since October 15, 1582 at 00:00:00.
*/
#define AppDelegateDefaultsKeyPersistentUniqueIdentifier @"AppDelegateDefaultsKeyPersistentUniqueIdentifier"
- (NSString*)persistentUniqueIdentifier
{
// try to read unique identifier from user defaults
NSString* result = [[NSUserDefaults standardUserDefaults] objectForKey:AppDelegateDefaultsKeyPersistentUniqueIdentifier];
if( result == nil )
{
// create new unique identifier
CFUUIDRef uuid = CFUUIDCreate( nil );
result = [(NSString*)CFUUIDCreateString( nil, uuid ) autorelease];
CFRelease( uuid );
// store new unique identifier in user defaults and synchronize them
[[NSUserDefaults standardUserDefaults] setObject:result forKey:AppDelegateDefaultsKeyPersistentUniqueIdentifier];
[[NSUserDefaults standardUserDefaults] synchronize];
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment