2

I know that the UDID is deprecated in iOS 5.0. Before I go crazy and update all my apps that have a [UIDevice currentDevice].uniqueIdentifier call, I would like to know if the iPhone 4S will report a UDID.

If it has a UDID, basically it will save me the trouble of updating my apps right away. If it doesn't have a UDID and basically terminates the app after the call for the UDID, then I will really need to update my apps right away.

Thanks in advance.

1
  • 2
    Every iPhone will have a UDID, it's just that Apple doesn't want developers using it to identify their users' devices.
    – BoltClock
    Oct 15, 2011 at 3:53

4 Answers 4

4

I would suggest changing over from uniqueIdentifier to this open source library (2 simple categories really). It utilizes the device’s MAC Address along with the App Bundle Identifier to generate a unique ID in your applications that can be used as a UDID replacement.

Keep in mind that unlike the UDID this number will be different for every app.

You simply need to import the included NSString and UIDevice categories and call:

#import "UIDevice+IdentifierAddition.h"
#import "NSString+MD5Addition.h"
NSString *iosFiveUDID = [[UIDevice currentDevice] uniqueDeviceIdentifier]

In order to grab the generated device identifier.

You can find it on Github here:

https://github.com/gekitz/UIDevice-with-UniqueIdentifier-for-iOS-5


Heres the code (just the .m files - check the github project for the headers):

UIDevice+IdentifierAddition.m

#import "UIDevice+IdentifierAddition.h"
#import "NSString+MD5Addition.h"

#include <sys/socket.h> // Per msqr
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>

@interface UIDevice(Private)

- (NSString *) macaddress;

@end

@implementation UIDevice (IdentifierAddition)

////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Private Methods

// Return the local MAC addy
// Courtesy of FreeBSD hackers email list
// Accidentally munged during previous update. Fixed thanks to erica sadun & mlamb.
- (NSString *) macaddress{
    
    int                 mib[6];
    size_t              len;
    char                *buf;
    unsigned char       *ptr;
    struct if_msghdr    *ifm;
    struct sockaddr_dl  *sdl;
    
    mib[0] = CTL_NET;
    mib[1] = AF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;
    
    if ((mib[5] = if_nametoindex("en0")) == 0) {
        printf("Error: if_nametoindex error\n");
        return NULL;
    }
    
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 1\n");
        return NULL;
    }
    
    if ((buf = malloc(len)) == NULL) {
        printf("Could not allocate memory. error!\n");
        return NULL;
    }
    
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2");
        return NULL;
    }
    
    ifm = (struct if_msghdr *)buf;
    sdl = (struct sockaddr_dl *)(ifm + 1);
    ptr = (unsigned char *)LLADDR(sdl);
    NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", 
                           *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    free(buf);
    
    return outstring;
}

////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public Methods

- (NSString *) uniqueDeviceIdentifier{
    NSString *macaddress = [[UIDevice currentDevice] macaddress];
    NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];  
    NSString *stringToHash = [NSString stringWithFormat:@"%@%@",macaddress,bundleIdentifier];
    NSString *uniqueIdentifier = [stringToHash stringFromMD5];  
    return uniqueIdentifier;
}

- (NSString *) uniqueGlobalDeviceIdentifier{
    NSString *macaddress = [[UIDevice currentDevice] macaddress];
    NSString *uniqueIdentifier = [macaddress stringFromMD5];    
    return uniqueIdentifier;
}

@end

NSString+MD5Addition.m:

#import "NSString+MD5Addition.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString(MD5Addition)

- (NSString *) stringFromMD5{
    
    if(self == nil || [self length] == 0)
        return nil;
    
    const char *value = [self UTF8String];
    
    unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5(value, strlen(value), outputBuffer);
    
    NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
        [outputString appendFormat:@"%02x",outputBuffer[count]];
    }
    return [outputString autorelease];
}

@end
2
  • That's a cool idea, but it's too advanced for what I'm trying to do. Thanks though
    – spybart
    Nov 1, 2011 at 2:00
  • 1 line shouldn't be to advanced. This: NSString *iosFiveUDID = [[UIDevice currentDevice] uniqueDeviceIdentifier]; is all you need to do after you download and include the 2 category files from the github link above.
    – chown
    Nov 1, 2011 at 2:56
3

It definitely still works - we've seen a bunch of purchases logged in our system today from iPhone 4S users whose devices presented valid UDIDs. (obtained via uniqueIdentifier) But Apple may very well remove it in iOS 6, so it's worth beginning to explore workarounds.

2
  • Thats Ertebolle, I guess I was really just unclear about the meaning of deprecating code
    – spybart
    Oct 15, 2011 at 3:55
  • 1
    Most of the time it just means that they're reserving the right to take it away eventually; they don't want a lot of apps to suddenly stop working, so they generally give a lot of advance warning. Some APIs that were actually deprecated in iOS 3 - [UITableViewCell initWithFrame:] for example - still work fine in iOS 5.
    – Ertebolle
    Oct 15, 2011 at 3:58
3

-[UIDevice uniqueIdentifier] will continue to work in iOS 5, but you should move to an alternative mechanism.

2
  • Dave, I know that the uniqueIdentifier will continue to work on current devices, but do you actually know if it will work on the iPhone 4S? Proof?
    – spybart
    Oct 15, 2011 at 3:37
  • Dave, last time I checked, it wasn't a requirement for developers to know who works on the UIKit team or not. Also, last time I checked, it isn't good practice to take random people's words on things without an actual explanation. Thanks for the answer but for future responses I recommend a more detailed explanation rather than letting your reputation do the talking.
    – spybart
    Nov 1, 2011 at 1:58
0

Although the [[UIDevice currentDevice] uniqueDeviceIdentifier] still works Apple has started rejecting apps that access the UDID's of devices due to security reasons,So better find another alternative to UDID. For more on this visit this link: http://techcrunch.com/2012/03/24/apple-udids/

1
  • I had an app approved just yesterday which accesses the users UDID
    – spybart
    Apr 2, 2012 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.