Wednesday, August 8, 2012

Alternative to iOS Device UUID

Since from iOS 5, Apple has deprecated the  UIDevice uniqueIdentifier , that means traditional way of getting the unique id of each iOS device won't work now  ie.
  [[UIDevice currentDevice] uniqueIdentifier]    fails from iOS 5 and more.


So for the alternative to the UUID , we can use the CFUUID class of Apple in order to create unique id for device. But, we really need to keep in mind that this inbuild class will create random numbers so they will return different ids on every call. Don't use NSUserDefaults for storing it, best way is to use Keychain.

So, here I am giving you the best way of using it in order to use it as a unique key for your device.

- (NSString *)createNewUUID {

    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}



You can than store it in KEYCHAIN on the very first launch. So that after first lunch, we can simply use it from keychain, no need to regenerate it.  The main reason for using Keychain to store is: When you set the UUID to the Keychain, it will persist even if the user completely uninstalls the App and then installs it again. . So, this is the permanent way of storing it, which means the key will be unique all the way.

Last Look :
Download and Drag SSKeychain.m and .h file to your project
#import "SSKeychain.h"
#import <Security/Security.h>

On applictaion lunch include the following code :

// getting the unique key (if present ) from keychain , assuming "your app identifier" as a key
NSString *retrieveuuid = [SSKeychain passwordForService:@"your app identifier" account:@"user"];

    if (retrieveuuid == nil) { // if this is the first time app lunching , create key for device
        NSString *uuid  = [self createNewUUID];
  // save newly created key to Keychain
        [SSKeychain setPassword:uuid forService:@"your app identifier" account:@"user"];
       // this is the one time process
    }
Note : Download SSKeychain.m and .h file from  https://github.com/samsoffes/sskeychain

1 comment:

  1. Include frameowork "Security.framework" to your project to import Security/Security.h

    ReplyDelete