Generating your own UDID

Update: I’ve since stumbled upon OpenUDID which is an even better solution.

Now that Apple are phasing out the use of UDID’s due to privacy reasons, developers are required to generate their own unique device identifiers. I’ve put together a quick method that you can utilize to generate a unique id and store it to the devices keychain using Apple’s KeyChainItemWrapper

- (NSString *)guid
{

    KeyChainItemWrapper *keychain = [[[KeychainItemWrapper alloc] initWithIdentifier:@"UDIDData" accessGroup:@"crossbow.com.alexfish.GenericKeychainSuite"] autorelease];
     
    NSString *guid = [keychain objectForKey:(id)kSecValueData];
         
    if(guid.length == 0)
    {
        CFUUIDRef uuid = CFUUIDCreate(NULL);
        CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
        CFRelease(uuid);
        guid = [(NSString *) uuidStr autorelease];
         
        [keychain setObject:guid forKey:(id)kSecValueData];
    }
     
    return guid;
      
}

This method checks if a generated ID already exists on the device and if not simply generate one, store it to the keychain then return it. It’s also worth noting that Apple’s code has a memory leak at KeyChainItemWrapper.m:196

self.keychainItemData = [[NSMutableDictionary alloc] init];

Should be..

self.keychainItemData = [[[NSMutableDictionary alloc] init] autorelease];

Thanks Apple!

2 notes 
Tags: UDID Apple iOS iphone development iOS 5.0
  1. pragmaticprogrammer reblogged this from alexefish
  2. amirsaam reblogged this from alexefish
  3. alexefish posted this