Get the device ID in iOS 6.0 and earlier

An updated version of this article for iOS7 is Get the device ID in iOS 7.0 and earlier

As you may know, starting from iOS 6.0 the method to retrieve the UDID ([[UIDevice currentDevice] uniqueIdentifier]) is deprecated.

This is due to privacy reasons. Allowing applications to detect the device will give ability to track the user without permission from him.

However is still possible to get a unique identifier for an AppStore publisher. A new method was introduced in iOS 6.0: [[[UIDevice currentDevice] identifierForVendor] UUIDString].

I’ve created the following code to get the identifier with compatibility in all versions of iOS. The code contains several tricks explained below.

// Get the device id on iOS6.0 and previous versions
- (NSString *) getDeviceId {
    NSString *deviceID;
    UIDevice *device = [UIDevice currentDevice];
    if ([UIDevice instancesRespondToSelector:@selector(identifierForVendor)]) {
        NSLog(@"%s DEBUG : iOS6.0 or later 'identifierForVendor' is available.", __func__);
        deviceID = [[device identifierForVendor] UUIDString];
    } else {
        NSLog(@"%s DEBUG : iOS5.x or older 'identifierForVendor' is not available.", __func__);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        deviceID = [device uniqueIdentifier];
#pragma clang diagnostic pop
    }
    return deviceID;
}

Three useful tricks contained in the code are explained below.

1) To check new iOS 6.0 method availability I use instancesRespondToSelector method, available for all objective-c classes.

2) To log debug messages with the current method I use __func__ variable, this allows fast identification of messages in debug console.

3) The use of deprecated method will give an annoying warning in xCode. To remove it I use the three pragma preprocessor directives that disables the warning only for the needed code line.

More informations on UDID APIs changes in iOS 6 can be found in this article UDID Replacement APIs in iOS 6