Khaytaah Khaytaah - 2 months ago 11
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:

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
// iOS 6.0 or later
NSString DeviceID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
#else
// iOS 5.X or earlier
NSString DeviceID = [[UIDevice currentDevice] uniqueIdentifier];
#endif

Answer

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 http://iosdevelopertips.com/device/determine-mac-address.html
    }
    return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
}
Comments