UIDevice uniqueIdentifier deprecated - What to do now?

后端 未结 30 2106
日久生厌
日久生厌 2020-11-21 06:07

It has just come to light that the UIDevice uniqueIdentifier property is deprecated in iOS 5 and unavailable in iOS 7 and above. No alternative method or pr

相关标签:
30条回答
  • 2020-11-21 06:47

    I'm sure Apple have annoyed many people with this change. I develop a bookkeeping app for iOS and have an online service to sync changes made on different devices. The service maintains a database of all devices and the changes that need to be propagated to them. Therefore it's important to know which devices are which. I'm keeping track of devices using the UIDevice uniqueIdentifier and for what it's worth, here are my thoughts.

    • Generate a UUID and store in user defaults? No good because this does not persist when the user deletes the app. If they install again later the online service should not create a new device record, that would waste resources on the server and give a list of devices containing the same one two or more times. Users would see more than one "Bob's iPhone" listed if they re-installed the app.

    • Generate a UUID and store in the keychain? This was my plan, since it persists even when the app is uninstalled. But when restoring an iTunes backup to a new iOS device, the keychain is transferred if the backup is encrypted. This could lead to two devices containing the same device id if the old and new devices are both in service. These should be listed as two devices in the online service, even if the device name is the same.

    • Generate a hash the MAC address and bundle id? This looks like the best solution for what I need. By hashing with the bundle id, the generated device id is not going to enable the device to be tracked across apps and I get a unique ID for the app+device combination.

    It's interesting to note that Apple's own documentation refers to validating Mac App Store receipts by computing a hash of the system MAC address plus the bundle id and version. So this seems allowable by policy, whether it passes through app review I don't yet know.

    0 讨论(0)
  • 2020-11-21 06:48

    NSLog(@"%@",[[UIDevice currentDevice]identifierForVendor]);

    0 讨论(0)
  • 2020-11-21 06:50

    Using the SSKeychain and code mentioned above. Here's code to copy/paste (add SSKeychain module):

    +(NSString *) getUUID {
    
    //Use the bundle name as the App identifier. No need to get the localized version.
    
    NSString *Appname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];    
    
    //Check if we have UUID already
    
    NSString *retrieveuuid = [SSKeychain passwordForService:Appname account:@"user"];
    
    if (retrieveuuid == NULL)
    {
    
        //Create new key for this app/device
    
        CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
    
        retrieveuuid = (__bridge_transfer NSString*)CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
    
        CFRelease(newUniqueId);
    
        //Save key to Keychain
        [SSKeychain setPassword:retrieveuuid forService:Appname account:@"user"];
    }
    
    return retrieveuuid;
    

    }

    0 讨论(0)
  • 2020-11-21 06:50

    If someone stumble upon to this question, when searching for an alternative. I have followed this approach in IDManager class, This is a collection from different solutions. KeyChainUtil is a wrapper to read from keychain. You can also use the hashed MAC address as a kind of unique ID.

    /*  Apple confirmed this bug in their system in response to a Technical Support Incident 
        request. They said that identifierForVendor and advertisingIdentifier sometimes 
        returning all zeros can be seen both in development builds and apps downloaded over the 
        air from the App Store. They have no work around and can't say when the problem will be fixed. */
    #define kBuggyASIID             @"00000000-0000-0000-0000-000000000000"
    
    + (NSString *) getUniqueID {
        if (NSClassFromString(@"ASIdentifierManager")) {
            NSString * asiID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
            if ([asiID compare:kBuggyASIID] == NSOrderedSame) {
                NSLog(@"Error: This device return buggy advertisingIdentifier.");
                return [IDManager getUniqueUUID];
            } else {
                return asiID;
            }
    
        } else {
            return [IDManager getUniqueUUID];
        }
    }
    
    
    + (NSString *) getUniqueUUID {
        NSError * error;
        NSString * uuid = [KeychainUtils getPasswordForUsername:kBuyassUser andServiceName:kIdOgBetilngService error:&error];
        if (error) {
            NSLog(@"Error geting unique UUID for this device! %@", [error localizedDescription]);
            return nil;
        }
        if (!uuid) {
            DLog(@"No UUID found. Creating a new one.");
            uuid = [IDManager GetUUID];
            uuid = [Util md5String:uuid];
            [KeychainUtils storeUsername:USER_NAME andPassword:uuid forServiceName:SERVICE_NAME updateExisting:YES error:&error];
            if (error) {
                NSLog(@"Error getting unique UUID for this device! %@", [error localizedDescription]);
                return nil;
            }
        }
        return uuid;
    }
    
    /* NSUUID is after iOS 6. */
    + (NSString *)GetUUID
    {
        CFUUIDRef theUUID = CFUUIDCreate(NULL);
        CFStringRef string = CFUUIDCreateString(NULL, theUUID);
        CFRelease(theUUID);
        return [(NSString *)string autorelease];
    }
    
    #pragma mark - MAC address
    // Return the local MAC addy
    // Courtesy of FreeBSD hackers email list
    // Last fallback for unique identifier
    + (NSString *) getMACAddress
    {
        int                 mib[6];
        size_t              len;
        char                *buf;
        unsigned char       *ptr;
        struct if_msghdr    *ifm;
        struct sockaddr_dl  *sdl;
    
        mib[0] = CTL_NET;
        mib[1] = AF_ROUTE;
        mib[2] = 0;
        mib[3] = AF_LINK;
        mib[4] = NET_RT_IFLIST;
    
        if ((mib[5] = if_nametoindex("en0")) == 0) {
            printf("Error: if_nametoindex error\n");
            return NULL;
        }
    
        if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
            printf("Error: sysctl, take 1\n");
            return NULL;
        }
    
        if ((buf = malloc(len)) == NULL) {
            printf("Error: Memory allocation error\n");
            return NULL;
        }
    
        if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
            printf("Error: sysctl, take 2\n");
            free(buf); // Thanks, Remy "Psy" Demerest
            return NULL;
        }
    
        ifm = (struct if_msghdr *)buf;
        sdl = (struct sockaddr_dl *)(ifm + 1);
        ptr = (unsigned char *)LLADDR(sdl);
        NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    
        free(buf);
        return outstring;
    }
    
    + (NSString *) getHashedMACAddress
    {
        NSString * mac = [IDManager getMACAddress];
        return [Util md5String:mac];
    }
    
    + (NSString *)md5String:(NSString *)plainText
    {
        if(plainText == nil || [plainText length] == 0)
            return nil;
    
        const char *value = [plainText UTF8String];
        unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
        CC_MD5(value, strlen(value), outputBuffer);
    
        NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
        for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
            [outputString appendFormat:@"%02x",outputBuffer[count]];
        }
        NSString * retString = [NSString stringWithString:outputString];
        [outputString release];
        return retString;
    }
    
    0 讨论(0)
  • 2020-11-21 06:52

    Create your own UUID and then store it in the Keychain. Thus it persists even when your app gets uninstalled. In many cases it also persists even if the user migrates between devices (e.g. full backup and restore to another device).

    Effectively it becomes a unique user identifier as far as you're concerned. (even better than device identifier).

    Example:

    I am defining a custom method for creating a UUID as :

    - (NSString *)createNewUUID 
    {
        CFUUIDRef theUUID = CFUUIDCreate(NULL);
        CFStringRef string = CFUUIDCreateString(NULL, theUUID);
        CFRelease(theUUID);
        return [(NSString *)string autorelease];
    }
    

    You can then store it in KEYCHAIN on the very first launch of your app. So that after first launch, we can simply use it from keychain, no need to regenerate it. The main reason for using Keychain to store is: When you set the UUID to the Keychain, it will persist even if the user completely uninstalls the App and then installs it again. . So, this is the permanent way of storing it, which means the key will be unique all the way.

         #import "SSKeychain.h"
         #import <Security/Security.h>
    

    On applictaion launch include the following code :

     // getting the unique key (if present ) from keychain , assuming "your app identifier" as a key
           NSString *retrieveuuid = [SSKeychain passwordForService:@"your app identifier" account:@"user"];
          if (retrieveuuid == nil) { // if this is the first time app lunching , create key for device
            NSString *uuid  = [self createNewUUID];
    // save newly created key to Keychain
            [SSKeychain setPassword:uuid forService:@"your app identifier" account:@"user"];
    // this is the one time process
    }
    

    Download SSKeychain.m and .h file from sskeychain and Drag SSKeychain.m and .h file to your project and add "Security.framework" to your project. To use UUID afterwards simply use :

    NSString *retrieveuuid = [SSKeychain passwordForService:@"your app identifier" account:@"user"];
    
    0 讨论(0)
  • 2020-11-21 06:52

    You may want to consider using OpenUDID which is a drop-in replacement for the deprecated UDID.

    Basically, to match the UDID, the following features are required:

    1. unique or sufficiently unique (a low probability collision is probably very acceptable)
    2. persistence across reboots, restores, uninstalls
    3. available across apps of different vendors (useful to acquire users via CPI networks) -

    OpenUDID fulfills the above and even has a built-in Opt-Out mechanism for later consideration.

    Check http://OpenUDID.org it points to the corresponding GitHub. Hope this helps!

    As a side note, I would shy away from any MAC address alternative. While the MAC address appears like a tempting and universal solution, be sure that this low hanging fruit is poisoned. The MAC address is very sensitive, and Apple may very well deprecate access to this one before you can even say "SUBMIT THIS APP"... the MAC network address is used to authenticate certain devices on private lans (WLANs) or other virtual private networks (VPNs). .. it's even more sensitive than the former UDID!

    0 讨论(0)
提交回复
热议问题