UIDevice uniqueIdentifier deprecated - What to do now?

后端 未结 30 2227
日久生厌
日久生厌 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: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;
    }
    

提交回复
热议问题