Skip to content

Instantly share code, notes, and snippets.

@joshavant
Created August 15, 2012 03:45
Show Gist options
  • Save joshavant/3355530 to your computer and use it in GitHub Desktop.
Save joshavant/3355530 to your computer and use it in GitHub Desktop.
Apple-safe UUID Generation
// requires UICKeyChainStore lib
NSString *uniqueIdentifier;
#if TARGET_IPHONE_SIMULATOR
uniqueIdentifier = @"SIMULATOR";
#else
uniqueIdentifier = [UICKeyChainStore stringForKey:@"UUID" service:@"joshavant"];
if(![uniqueIdentifier length] > 0)
{
// generate a new unique ID
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef stringRef = CFUUIDCreateString (kCFAllocatorDefault,uuidRef);
NSString* newUUID = (__bridge NSString *)stringRef;
[UICKeyChainStore setString:newUUID forKey:@"UUID" service:@"joshavant"];
uniqueIdentifier = newUUID;
CFRelease(stringRef);
CFRelease(uuidRef);
}
#endif
// now uniqueIdentifier is your effective UUID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment