Skip to content

Instantly share code, notes, and snippets.

@chillpop
Created May 17, 2013 15:25
Show Gist options
  • Save chillpop/5599792 to your computer and use it in GitHub Desktop.
Save chillpop/5599792 to your computer and use it in GitHub Desktop.
Create, store, and retrieve a unique iOS device ID using the appropriate technology.
//get a unique ID
NSString *deviceID = nil;
UIDevice *device = [UIDevice currentDevice];
if ([device respondsToSelector:@selector(identifierForVendor)]) {
deviceID = [[device identifierForVendor] UUIDString];
}
else {
//retrieve an NSDefaults stored UUID
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uuidKey = @"UUID";
deviceID = [defaults stringForKey:uuidKey];
//nothing stored yet
if (deviceID == nil) {
//create a new UUID
CFUUIDRef cfuuid = CFUUIDCreate(NULL);
CFStringRef cfDeviceID = CFUUIDCreateString(NULL, cfuuid);
deviceID = (NSString *)cfDeviceID;
CFRelease(cfuuid);
//autorelease the string after passing the uuid to NSDefaults
//CFRelease(cfDeviceID);
NSLog(@"creating new UUID: %@", deviceID);
//and store it in NSDefaults
[defaults setObject:deviceID forKey:uuidKey];
[deviceID autorelease];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment