任何人都可以知道如何将UDID转换为UUID(40digit到32digit)

时间:2013-07-18 09:09:43

标签: iphone ios

如何使用ObjectiveC以下列格式将UDID转换为UUID(40位到32位)?

[0C50D390-DC8E-436B-8AD0-A36D1B304B18]
[8-4-4-4-12]

4 个答案:

答案 0 :(得分:1)

转换???不,您无法将UDID转换为UUID。 Apple已从编程方面删除了UDID。从2013年5月1日起,Apple已开始拒绝访问UniqueIdentifier的应用程序。

相反,您可以从代码中创建UUID,该代码将为您提供[8-4-4-4-12]格式的标识符。要生成UUID,您可以使用CFUUID class,如下所示:

CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidString = (NSString *)CFUUIDCreateString(NULL,uuidRef);
CFRelease(uuidRef);

NSLog(@"%@",uuidString);

然后,请将uuidString保存在UserDefaults或db中,因为您无法再次生成相同的uuidString

答案 1 :(得分:0)

如你所知苹果在iOS 5中被弃用了UDID,但是有一种很好的方法可以使用MD5算法生成32位哈希值。为此需要提供Wifi-Mac地址,然后MD5将转换为32位哈希值。如果您重新安装应用程序,使用此UDID也不会更改。

注意:Apple不会接受功能中的Wifi-Mac地址(iOS7)。一旦iOS 7出现在图片中,您将无法访问Wifi-Mac地址。即使您访问您的应用程序,苹果也会拒绝您的应用程序。

答案 2 :(得分:0)

您可以选择以下方法,

如果有人在找到替代方案时偶然发现了这个问题。我在IDManager课程中遵循了这种方法, 这是来自不同解决方案的集合。 KeyChainUtil是一个从keychain读取的包装器。 您还可以将hashed MAC address用作一种唯一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;
}

答案 3 :(得分:0)

  1. 根本无法将UDID转换为UUID。这不是Apple的可行和限制。
  2. 您可以创建自己的UUDI,也可以通过在应用程序中编写一个小代码来生成它。
  3. 要创建我们自己的唯一标识符,我们使用Core Foundation函数CFUUIDCreate()。它返回对opaque类型CFUUIDRef的引用,我们可以将其转换为带有CFUUIDCreateString函数的字符串。

    最好将这些调用包装在一个小的NSString类别中,以便我们可以轻松地重用它们(在许多情况下UUID会派上用场,例如,如果你需要生成唯一的文件名):

    @interface NSString (UUID)
    
    + (NSString *)uuid;
    
    @end
    
    
    @implementation NSString (UUID)
    
    + (NSString *)uuid
    {
        NSString *uuidString = nil;
        CFUUIDRef uuid = CFUUIDCreate(NULL);
        if (uuid) {
            uuidString = (NSString *)CFUUIDCreateString(NULL, uuid);
            CFRelease(uuid);
        }
        return [uuidString autorelease];
    }
    
    @end
    

    现在,在我们的代码中,我们在首次启动应用程序时生成一个新的UUID,并将其存储到用户默认数据库中:

      #define UUID_USER_DEFAULTS_KEY @"UUID"
    
        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        {
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            if ([defaults objectForKey:UUID_USER_DEFAULTS_KEY] == nil) {
                [defaults setObject:[NSString uuid] forKey:UUID_USER_DEFAULTS_KEY];
                [defaults synchronize];
            }
    
        ...
    

    希望这有助于澄清您对UUID的疑虑。一切顺利!