10

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
Arun_
  • 1,806
  • 2
  • 20
  • 40
Khaytaah
  • 103
  • 1
  • 4

3 Answers3

6

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];
}
Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71
  • Thanks Grzegorz Krukowski. Still getting: "No visible @interface for 'UIDevice' declares the selector 'uniqueIdentifier'" error because Apple removed the uniqueIdentifier property from the UIDevice class. – Khaytaah Sep 12 '13 at 17:17
  • Ok than modify that part: return [[UIDevice currentDevice] uniqueIdentifier]; for some other UID in that case :) either generate on your own or use UUID in that case or get MACAddress - updated my answer – Grzegorz Krukowski Sep 12 '13 at 19:13
  • Ok I found another safer way to use respondsToSelector and performSelector - that should not raise errors. – Grzegorz Krukowski Sep 12 '13 at 19:19
  • Thanks Grzegorz Krukowski! – Khaytaah Sep 17 '13 at 10:11
  • 3
    Apple no longer allow the user of `ASIdentifierManager` without any advertisements in the app. You will be rejected if you use `ASIdentifierManager` without showing a banner. – rckoenes Mar 04 '14 at 11:19
1

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

Community
  • 1
  • 1
B.S.
  • 21,660
  • 14
  • 87
  • 109
0

As hard replacement of static macro, you can try dynamic if statement to check it.

UIDevice has property named 'systemVersion' and you can check this.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
youknowone
  • 919
  • 6
  • 14