10

I am trying to activate APNS. Because uniqueIdentifier is deprecated, I'm trying to use CFUUIDCreate with the following code:

UIDevice *dev = [UIDevice currentDevice];
NSString *deviceUuid;
if ([dev respondsToSelector:@selector(uniqueIdentifier)])
    deviceUuid = dev.uniqueIdentifier;
else {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    id uuid = [defaults objectForKey:@"deviceUuid"];
    if (uuid)
        deviceUuid = (NSString *)uuid;
    else {
        CFStringRef cfUuid = CFUUIDCreateString(NULL, CFUUIDCreate(NULL));
        deviceUuid = (__bridge NSString *)cfUuid;
        CFRelease(cfUuid);
        [defaults setObject:deviceUuid forKey:@"deviceUuid"];
    }
}

How can I replace uniqueIdentifier with CFUUIDCreate?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
filou
  • 1,609
  • 5
  • 26
  • 53
  • Everything you need should be here: http://stackoverflow.com/questions/6993325/uidevice-uniqueidentifier-deprecated-what-to-do-now. Try more googling next time. – Majster May 11 '12 at 17:19
  • I already read all posts on stack.. the problem is: I don't know how to implement `CFUUIDCreate` in this case. – filou May 11 '12 at 17:31
  • I tried to edit your question to distinguish it from the previously-asked question, but it's not clear what you're asking. What's wrong with the code you provide above? Does it not compile, or does it not function as expected? – Brad Larson May 11 '12 at 17:51
  • The code works fine but `uniqueIdentifier` seems depredated and I don't want to get my app rejected.. – filou May 11 '12 at 17:54

1 Answers1

26

 iOS < 6.0

// Create universally unique identifier (object)
CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
    
// Get the string representation of CFUUID object.
NSString *uuidStr = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);
CFRelease(uuidObject);

iOS >= 6.0

NSString* UDID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
Community
  • 1
  • 1
Dima
  • 23,484
  • 6
  • 56
  • 83
  • Is it possible that the UUID i get from `CFUUIDRef` (6BC0A30D-11B7-4A6E-A3E2-763A2F1F0804) is completely different to the one of `uniqueIdentifier` (2f5aa38b6a903303a5bd13950b9c4807bf60e8f2)? o.o – filou May 11 '12 at 18:12
  • Yeah it's not going to be the same, it's just guaranteed to be unique. – Dima May 11 '12 at 18:16
  • 1
    This code is legen - wait for it -daaaary.. get your reputation mate! – filou May 11 '12 at 18:23
  • @lolcat Does `CFUUIDCreateString` follow create rule.. Do I need to call `CFRelease(uuidStr);` after saving the string? – Krishnabhadra May 30 '12 at 11:22
  • you should call `CFRelease(uuidStr);` as soon after the second line as you can (after assigning a string to the UUID). – Dima Jun 04 '12 at 14:00
  • 6
    As of iOS 6 you can use: NSString *UUID = [[NSUUID UUID] UUIDString]; – Symmetric Nov 19 '12 at 18:41
  • 1
    @Symmetric : That will always diffrent when reinstall application, You can use [[[UIDevice currentDevice] identifierForVendor] UUIDString] instead of [[NSUUID UUID] UUIDString]. – Ashvin Mar 07 '13 at 05:59
  • 1
    @John, that is actually incorrect. As you can see in the documentation for [UIDevice here](http://developer.apple.com/library/ios/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/doc/uid/TP40006902-CH3-SW49), _The value changes when the user deletes all of that vendor’s apps from the device and subsequently reinstalls one or more of them._ I would say having leftover apps from the same vendor is actually an edge case for most people, so the UUID will change regardless. – Dima Jun 01 '13 at 18:33
  • So can we use this Strategy for identifying the iOS Device ? Please suggest. – Sri Krishna Mar 24 '16 at 11:28
  • 1
    @SriKrishna yes but with limitations. In Particular, this ID will change if the user uninstalls all apps from a particular vendor. [Documentation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/#//apple_ref/occ/instp/UIDevice/identifierForVendor) – Dima Mar 24 '16 at 16:49