iOS开源项目:UIDevice-with-UniqueIdentifier-for-iOS-5

用于替代系统UDID的方法。

https://github.com/gekitz/UIDevice-with-UniqueIdentifier-for-iOS-5

1、使用方法:

   UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 100)];
    label.numberOfLines = 0;
    label.textAlignment = UITextAlignmentCenter;
    label.text = [NSString stringWithFormat:@"Unique Device Identifier:\n%@",
                  [[UIDevice currentDevice] uniqueDeviceIdentifier]];
    
    [self.view addSubview:label];
    [label release];
                  
    label = [[UILabel alloc] initWithFrame:CGRectMake(10, 210, 300, 100)];
    label.numberOfLines = 0;
    label.textAlignment = UITextAlignmentCenter;
    label.text = [NSString stringWithFormat:@"Unique GLOBAL Device Identifier:\n%@",
                  [[UIDevice currentDevice] uniqueGlobalDeviceIdentifier]];
                  
    [self.view addSubview:label];
    [label release];

只需要调用

[[UIDevice currentDevice] uniqueDeviceIdentifier]//Bundle ID + MAC address,可以区分同一设备,不同App

[[UIDevice currentDevice] uniqueGlobalDeviceIdentifier]// MAC address设备表示,所有App都相同

2、实现原理:

- (NSString *) uniqueDeviceIdentifier{
    NSString *macaddress = [[UIDevice currentDevice] macaddress];
    NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
    
    NSString *stringToHash = [NSString stringWithFormat:@"%@%@",macaddress,bundleIdentifier];
    NSString *uniqueIdentifier = [stringToHash stringFromMD5];
    
    return uniqueIdentifier;
}

- (NSString *) uniqueGlobalDeviceIdentifier{
    NSString *macaddress = [[UIDevice currentDevice] macaddress];
    NSString *uniqueIdentifier = [macaddress stringFromMD5];
    
    return uniqueIdentifier;
}

首先要获取mac地址,这东西其实不复杂,所以不要被吓着了。就是调用一下操作系统底层获取mac地址的方法就行,把这段代码贴出来以防以后用着的时候忘了:

- (NSString *) macaddress{
    
    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("Could not allocate memory. error!\n");
        return NULL;
    }
    
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2");
        free(buf);
        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;
}

然后就是生成16进制的标识符了

- (NSString *) stringFromMD5{
    
    if(self == nil || [self length] == 0)
        return nil;
    
    const char *value = [self UTF8String];
    NSString *string = [NSString stringWithUTF8String:value];
    NSLog(@"value = %@", string);
    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]];
    }
    
    return [outputString autorelease];
}

 

posted @ 2013-06-20 15:50  shangdahao  阅读(872)  评论(1编辑  收藏  举报