Thursday, 23 January 2014

Unique Identifier UUID - iOS

UDID is a hash value composed from various hardware identifiers such as the device serial number.
It is unique for each device. 
The UDID is independent of the device name, SIM card.
It is really easy to get it from the device programmatically:

NSString *uuid =  [[UIDevice currentDevice] uniqueIdentifier];

But UDID is no longer available onwards iOS 6 due to security / privacy reasons.
UDID is deprecated by Apple; developers can not make use of it in any iOS application.


So what now? How can we identify the device?

I found many solutions on searching over internet like identifierForVendor or advertisingIdentifier or generate our own UUID (store & persist it on your own).

Many examples show how to generate UUID and store it in NSUserDefault:

-(NSString *)generateUUID {
    NSString *CFUUID = nil;
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"UUID"] == nil) {
        CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
        CFUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuid));
        [[NSUserDefaults standardUserDefaults] setValue:CFUUID forKey:@"UUID"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    } else {
        CFUUID = [[NSUserDefaults standardUserDefaults] valueForKey:@"UUID"];
    }

But if you store the UUID in NSUserDefaults you would loose it as soon as the app gets uninstalled from the device.
So the better idea is to store it in Keychain. In case the app is uninstalled Keychain will preserve the UUID and on next installation we can fetch it from there.

Create & Store UUID in Keychain: Source Code


4 comments:

  1. Thank you Suraj, your detail explain and sample source is very helpful to me~

    ReplyDelete
  2. how to find udid..not uuid?

    ReplyDelete
    Replies
    1. We can retrieve UDID programmatically, but as Apple says you cannot use UDID in your app on iTunesConnect.
      If they detect it your app, it will be rejected straightaway.

      You can find UDID using below link, this can be used to add a device to Apple Developer Portal for testing/development:
      http://www.igeeksblog.com/how-to-find-iphone-udid-number/

      Delete
  3. Hi Suraj,

    its works fine , can i known what to add if i need to access keychain between two apps using FDkeychain .

    Thanks
    Yashwanth

    ReplyDelete