Khaytaah Khaytaah - 6 months ago 30
Objective-C Question

iOS7 app backward compatible with iOS5 regarding unique identifier

My app is compatible with iOS5 and iOS6.
Until now I had no problem using:

NSString DeviceID = [[UIDevice currentDevice] uniqueIdentifier];

Now with iOS7 and with uniqueIdentifier not working anymore I changed to:

NSString DeviceID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

The problem is, this would not work for iOS5.

How can I achieve backward compatibility with iOS5?

I tried this, with no luck:

// iOS 6.0 or later
NSString DeviceID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
// iOS 5.X or earlier
NSString DeviceID = [[UIDevice currentDevice] uniqueIdentifier];


The best and recommend option by Apple is:

 NSString *adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

Use it for every device above 5.0.

For 5.0 you have to use uniqueIdentifier. The best to check if it's available is:

if (!NSClassFromString(@"ASIdentifierManager"))

Combining that will give you:

- (NSString *) advertisingIdentifier
    if (!NSClassFromString(@"ASIdentifierManager")) {
        SEL selector = NSSelectorFromString(@"uniqueIdentifier");
        if ([[UIDevice currentDevice] respondsToSelector:selector]) {
            return [[UIDevice currentDevice] performSelector:selector];
        //or get macaddress here
    return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];