Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 243
Khaytaah Avatar asked Sep 12 '13 16:09

Khaytaah


2 Answers

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];
}
like image 105
Grzegorz Krukowski Avatar answered Oct 29 '22 14:10

Grzegorz Krukowski


Why just not to use CFUUIDRef and be independent with iOS verion?

CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);

self.uuidString = (NSString *)CFUUIDCreateString(NULL,uuidRef);

CFRelease(uuidRef);

And of course remember calculated uuidString in the Keychain(in case of application removal)?

Here is written how to use keychain

like image 20
B.S. Avatar answered Oct 29 '22 13:10

B.S.