How can i generate and use unique deviceId for iOS devices?

2.1k views Asked by At

In my previous app,I was using the below mentioned code to generate unique deviceId for iOS devices.But the problem is that it will generate a new code everytime when the app is reinstalled.How can i do this properly?

-(NSString*)uniqueIDForDevice //new..
{
    NSString* uniqueIdentifier = nil;
    if( [UIDevice instancesRespondToSelector:@selector(identifierForVendor)] ) { // >=iOS 7
        uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    }
    else
    { //<=iOS6, Use UDID of Device
        CFUUIDRef uuid = CFUUIDCreate(NULL);
        //uniqueIdentifier = ( NSString*)CFUUIDCreateString(NULL, uuid);- for non- ARC
        uniqueIdentifier = ( NSString*)CFBridgingRelease(CFUUIDCreateString(NULL, uuid));// for ARC
        CFRelease(uuid);
    }

    return uniqueIdentifier;
}
4

There are 4 answers

0
abhimuralidharan On BEST ANSWER

use This link to UICKeyChainStore . I have added the following method call

[self getDeviceIdFromKeychain]; in appDelegate applicationdidFinishLaunchingWithOptions

-(void)getDeviceIdFromKeychain
{
    NSString *deviceID = [UICKeyChainStore stringForKey:DEVICE_ID_FROM_KEYCHAIN_REFERENCE_APP service:nil];
    NSLog(@"deviceID from Keychain = (%@)",deviceID);

    //if vandorid from keychain is not nil
    if (deviceID)
    {
        [[NSUserDefaults standardUserDefaults] setObject:deviceID forKey:DEVICE_ID];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
//else it goes for new vendorid and then stored it to keychan
else if (deviceID == (id)[NSNull null] || deviceID.length == 0 )
{
    NSString *newDeviceId;
    newDeviceId=[[self class] deviceUUID];
    [[NSUserDefaults standardUserDefaults] setObject:newDeviceId forKey:DEVICE_ID];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [UICKeyChainStore setString:newDeviceId forKey:DEVICE_ID_FROM_KEYCHAIN_REFERENCE_APP  service:nil];
    NSLog(@"new deviceID = (%@)",newDeviceId);
}

}
+ (NSString *)deviceUUID
{
    if([[NSUserDefaults standardUserDefaults] objectForKey:[[NSBundle mainBundle] bundleIdentifier]])
        return [[NSUserDefaults standardUserDefaults] objectForKey:[[NSBundle mainBundle] bundleIdentifier]];

    @autoreleasepool {

        CFUUIDRef uuidReference = CFUUIDCreate(nil);
        CFStringRef stringReference = CFUUI

DCreateString(nil, uuidReference);
        NSString *uuidString = (__bridge NSString *)(stringReference);
        [[NSUserDefaults standardUserDefaults] setObject:uuidString forKey:[[NSBundle mainBundle] bundleIdentifier]];
        [[NSUserDefaults standardUserDefaults] synchronize];
        CFRelease(uuidReference);
        CFRelease(stringReference);
        return uuidString;
    }
}

And i use them like this...(in my signinViewcontroller)

NSString *deviceIDfromNSUserDefaults = [[NSUserDefaults standardUserDefaults]valueForKey:DEVICE_ID ];
    NSLog(@"deviceID from NSUserDefaults = (%@)",deviceIDfromNSUserDefaults);
1
iAsh On

I am using UUID to generate unique number. I doubt whether it is allowed to use vender ID if we are not using iAds in app. I have achieved this by generating UUID and then storing into the keychain. Keychain will store your UUID till user will not do Hard reset which is rare case.

As no API is provided by Apple to get unique device ID we have to find ways like this.

3
Saurabh Prajapati On

first of all get vendorId(uniqueID) than store it to keychain

// Fetch vendorID from keychain first, if it exists then use it or fetch vendorID 

//if user delete app and again install app than he get vendorID from keychain(if he hasn't cleared it)

NSString *vendorID = [UICKeyChainStore stringForKey:@"KEY TO SAVE TO Keychain" service:nil];
NSLog(@"VendorID from Keychain - %@",vendorID);

//if vandorid from keychain is not nil
if (vendorID)
{
    [[NSUserDefaults standardUserDefaults] setObject:vendorID forKey:@"vendorId"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

//else it goes for new vendorid and then stored it to keychan
else
{
    vendorID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

    [UICKeyChainStore setString:vendorID forKey:@"KEY TO SAVE TO Keychain"  service:nil];

    [[NSUserDefaults standardUserDefaults] setObject:vendorID forKey:@"vendorId"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSLog(@"VendorID Local - %@",vendorID);
}

i use this class to save and retrive vendorID from keyChain!

just take one look at this.hope it will help you

2
Tapas Pal On

You can use the below

+ (NSString *)deviceUUID
{
    if([[NSUserDefaults standardUserDefaults] objectForKey:[[NSBundle mainBundle] bundleIdentifier]])
        return [[NSUserDefaults standardUserDefaults] objectForKey:[[NSBundle mainBundle] bundleIdentifier]];

    @autoreleasepool {

        CFUUIDRef uuidReference = CFUUIDCreate(nil);
        CFStringRef stringReference = CFUUIDCreateString(nil, uuidReference);
        NSString *uuidString = (__bridge NSString *)(stringReference);
        [[NSUserDefaults standardUserDefaults] setObject:uuidString forKey:[[NSBundle mainBundle] bundleIdentifier]];
        [[NSUserDefaults standardUserDefaults] synchronize];
        CFRelease(uuidReference);
        CFRelease(stringReference);
        return uuidString;
    }
}