8

I'm using [[UIDevice currentDevice] uniqueIdentifier] in all of my apps, Apple is not allowing the use of uniqueIdentifier anymore. I need something to use that replaces the uniqueIdentifier which I can use to recognize a user even when the user deletes the app and installs it again, (and also get my app approved by apple).

Thanks

Joe
  • 56,979
  • 9
  • 128
  • 135
user784625
  • 1,928
  • 5
  • 24
  • 38
  • 3
    Possible duplicated - [UIDevice uniqueIdentifier Deprecated - What To Do Now?](http://stackoverflow.com/q/6993325/194544) – beryllium Feb 22 '12 at 14:06

3 Answers3

12

The documentation recommends what to do in this section.

Special Considerations
Do not use the uniqueIdentifier property. To create a unique identifier specific to your app, you can call the CFUUIDCreate function to create a UUID, and write it to the defaults database using the NSUserDefaults class.

To make sure that the unique identifier remains after you delete the app you should store it in the keychain rather than NSUserDefaults. Using the keychain you will also be able to share the same unique ID across all of your apps on the same device using keychain access groups. This approach will prevent you from incorrectly tracking users after the device is no longer theirs, and it will be available on any new iDevice they restore from backup.

Joe
  • 56,979
  • 9
  • 128
  • 135
  • you mean I can add to the device keychain the UUID and delete the app and when I install it again I retrieve the value and use it again? – user784625 Feb 22 '12 at 14:12
  • Yes, keychain items remain persistent (as long as the device is not wiped and setup as a new device), and when you use access groups you can allow other applications of yours to access the same keychain value. – Joe Feb 22 '12 at 14:26
  • one more question, can I use the MAC address istead and still get my app approved by apple? – user784625 Feb 22 '12 at 14:31
  • 1
    I believe so, that looks like a decent method. You should also think about how you want to track this unique id. If someone for example sells their iDevice to someone else and your using the MAC address alone you will be treating 2 different users as the same (which may or may not be what you want). – Joe Feb 22 '12 at 14:34
  • @Shantanu That is interesting because right at the top of the site it reads: *With the release of iOS 6 last year, Apple made great strides towards offering a powerful, secure UDID replacement. Now that iOS 6 makes up the vast majority of active iOS usage and in concert with Apple's official deprecation of the UDID, it is time to retire SecureUDID as well. We recommend using Apple's iOS 6 vendor and advertising identifiers instead.* – Joe May 15 '13 at 12:10
  • @Joe:Thanks for making me notice that. Next time, I will read thoroughly before posting.Good for me,as well as good for others :) – Shantanu May 20 '13 at 13:30
3

Update for iOS 7 and prior:

+ (NSString *)uniqueDeviceIdentifier
{
    NSString *device_id = nil;

    if ([[self deviceModel] isEqualToString:@"Simulator iOS"]) {
        // static id for simulator
        device_id = @"== your random id ==";
    }
    else if (CurrentIOSVersion >= 6.f) {
        // iOS 6 and later
        device_id = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    }
    else {
        // iOS 5 and prior
        SEL udidSelector = NSSelectorFromString(@"uniqueIdentifier");
        if ([[UIDevice currentDevice] respondsToSelector:udidSelector]) {
            device_id = [[UIDevice currentDevice] performSelector:udidSelector];
        }
    }
    NSLog(@">>>>>> device_id: %@", device_id);
    return device_id;
}

Device model you can receive through:

+ (NSString*)deviceModel
{
    static NSString *device_model = nil;

    if (device_model != nil)
        return device_model;

    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *str = @(systemInfo.machine);

    return device_model;
}
digipeople
  • 752
  • 6
  • 9
  • The problem with this code is that it will return a different value each time it's called on iOS 6 or later. To be a `uniqueIdentifier` replacement, it needs to be persistent. – kamprath Aug 12 '13 at 01:35
0

with digipeople's hack.

Fix it to avoid app crash.

systemId = [[NSUUID UUID] UUIDstring];

http://developer.apple.com/library/mac/documentation/Foundation/Reference/NSUUID_Class/Reference/Reference.html#//apple_ref/occ/instm/NSUUID/UUIDString

이성수
  • 1
  • 1