Skip to content

Instantly share code, notes, and snippets.

@kara-ryli
Created October 20, 2011 22:21
Show Gist options
  • Save kara-ryli/1302561 to your computer and use it in GitHub Desktop.
Save kara-ryli/1302561 to your computer and use it in GitHub Desktop.
Create a device-specific identifier in Objective-C
@implementation MyAppDelegate
/**
* Generates a Universally Unique Identifier based on the time and
* hardware details of a device. CFUUIDCreate is the recommended
* way by Apple to generate an ID now that UIDevice-uniqueIdentifier
* is deprecated.
*
* Props: http://www.cocoabuilder.com/archive/cocoa/217665-how-to-create-guid.html#217668
*/
- (NSString *)getDeviceID
{
static NSString *idKey = @"deviceId";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uuid = [defaults objectForKey:idKey];
if (!uuid) {
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
uuid = [NSString stringWithString:(NSString *) uuidStringRef];
CFRelease(uuidStringRef);
[defaults setObject:[uuid copy] forKey:idKey];
}
return uuid;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment