12

I want to get the unique identifier which should support all iOS versions..Can any one help me on this issue. As you know that apple is deprecated the UDID method, So there is possibility to generate Unique id using wifi-mac address.But apple is going to remove the wifi mac address in iOS7 version.So my requirement is to generate a new unique code which should work in all iOS versions.Thanks in advance..

Note: Don't change the UDID once user restart the device or reinstall the application.

Ganesh
  • 524
  • 1
  • 4
  • 16
  • There is a reason they are deprecating these kinds of unique ids. Create and store your own UUID (`CFUUIDCreate`) or use the vendor identifier – David Rönnqvist Jul 19 '13 at 09:55

3 Answers3

35

I was updating my application that was working based only on Unique Identifier which supported iOS 4.3 and above. So,

1) I was unable to use [UIDevice currentDevice].uniqueIdentifier; as it was no longer available

2) I could not use [UIDevice currentDevice].identifierForVendor.UUIDString because it was Available in iOS 6.0 and later only and was unable to use for lower iOS versions.

3) The mac address was not an option as it wasn't allowed in iOS-7

4) OpenUDID was deprecated some time ago and also had issues with iOS-6.

5) Advertisement identifiers were also not available for iOS-5 and below

Finally this was what i did

a) Added SFHFKeychainUtils to the project

b) Generated CFUUID key String

 CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);
    udidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid));

c) Saved it to Key Chain Utils or else it will generate a new Unique Each Time

Final Code

+ (NSString *)GetDeviceID {
    NSString *udidString;
   udidString = [self objectForKey:@"deviceID"];
    if(!udidString)
    {
    CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);
    udidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid));
    CFRelease(cfuuid);
        [self setObject:udidString forKey:@"deviceID"];
    }
    return udidString;
}

+(void) setObject:(NSString*) object forKey:(NSString*) key
{
    NSString *objectString = object;
    NSError *error = nil;
    [SFHFKeychainUtils storeUsername:key
                         andPassword:objectString
                      forServiceName:@"LIB"
                      updateExisting:YES
                               error:&error];

    if(error)
        NSLog(@"%@", [error localizedDescription]);
}

+(NSString*) objectForKey:(NSString*) key
{
    NSError *error = nil;
    NSString *object = [SFHFKeychainUtils getPasswordForUsername:key
                                                  andServiceName:@"LIB"
                                                           error:&error];
    if(error)
        NSLog(@"%@", [error localizedDescription]);

    return object;
}

enter image description here

For further Details

Quamber Ali
  • 2,170
  • 25
  • 46
  • 1
    Thank you for this. Please note the creator of SFHFKeychainUtils says it should be considered deprecated and one should use his STUtils instead. Also, should also add `CFRelease(cfuuid);` to the `GetDeviceID` method above to prevent a memory leak. – ghr Apr 04 '14 at 01:59
  • @ghr. Yes he says that it should be considered deprecated however i am using it. in many application without ay issue. for Second Thanks for that i somehow skipped that. – Quamber Ali Apr 15 '14 at 04:40
  • @NSQuamber.java, will this generated id be changed after uninstallation of the app? I have a requirement which states that the id must be unique and will be sent to server. Even the user uninstalls / reinstalls the app, I should have the same id. Is this possible with your solution ? Plus, is it still valid for iOS 8.4 ? – shamaleyte Aug 22 '15 at 11:18
  • 2
    Feeling like Apple hates developers.. :-) – Chanchal Raj Oct 09 '15 at 13:10
  • @shamaleyte Were you able to test this ? I also need to get a unique identifier for the device which won't change after the app has been reinstalled – Moumou Nov 10 '15 at 09:38
  • @kimimsc , unfortunately the current limitations still stand. no way of having a unique identifier including reinstallation . Please share if you find a way of doing it. Still feeling desperate about it :( – shamaleyte Nov 10 '15 at 15:37
  • @shamaleyte I've found a way of getting the actual device's UDID but am not sure if it would be allowed on the app store. I'll write a small tutorial about it and post the link to it here when it's done :) – Moumou Nov 19 '15 at 10:50
  • @kimimsc; will be looking forward to your tutorial. – shamaleyte Nov 19 '15 at 13:21
1

Now Device Identifier change to UUID.You can get UUID With the help of following code:

- (NSString *)getUUID
{
    NSString *UUID = [[NSUserDefaults standardUserDefaults] objectForKey:@"uniqueID"];
    if (!UUID) {
        CFUUIDRef theUUID = CFUUIDCreate(NULL);
        CFStringRef string = CFUUIDCreateString(NULL, theUUID);
        CFRelease(theUUID);
        UUID = [(__bridge NSString*)string stringByReplacingOccurrencesOfString:@"-"withString:@""];
        [[NSUserDefaults standardUserDefaults] setValue:UUID forKey:@"uniqueID"];
    }
    return UUID;
}

It's Work in all iOS version.

Community
  • 1
  • 1
Bond
  • 328
  • 4
  • 16
  • Yes, The UDID is getting changed once reinstall the application..This logic is not full fill my requirement. Any way Thanks for your logic. – Ganesh Jul 16 '13 at 13:57
-3

I don't have access to the code right now (can post in a few hours if you still need it) but what I've done is create a static method 'deviceIdentifier' in a helper class.

the method does a basic check for the current iOS version, returns UDID if below 6.0 and uniqueIdentifier otherwise

Let me know if you'd like the code for that and I'll post it when I can..it's only 10-15 lines or so if I remember right but makes a big difference as then you can just call '[myHelper deviceIdentifier]' wherever you need a device ID and not have to worry about which iOS version they are using

James Gupta
  • 871
  • 7
  • 15
  • Thank you so much for your quick information.. As you know that apple is deprecated the UDID method, So i implemented to generate a unique identifier using Mac address. But apple is going to remove the wifi mac address in iOS7 version.So i need to generate a new unique code which should work in all the versions including iOS7.Can you please share your logic so that will helps me a lot.. Once again thank you.. – Ganesh Jul 16 '13 at 13:32
  • Hi Gahesh - just double checked my code and remembered I took pre-identifierForVendor code out of there completely just to ensure compliance with Apple guidelines, i think they're being pretty strict about people using UDID in new apps even just to support older versions of iiOS – James Gupta Jul 16 '13 at 14:51
  • So I'm just using identifierForVendor which I understand wont fulfil your requirement that it must remain constant even in app uninstall – James Gupta Jul 16 '13 at 14:51
  • Hi James, idenetifierForVendor is available from iOS 6. But using this we can support iOS6 or above devices.Here my question is how to support lower versions. I am strongly believe there is a solution for this. Lets wait and see what will come to the picture :) – Ganesh Jul 17 '13 at 05:55