Created
May 17, 2013 15:25
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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