用于替代系统UDID的方法。

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

1、使用方法:

   UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
label.numberOfLines = ;
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(, , , )];
label.numberOfLines = ;
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[];
size_t len;
char *buf;
unsigned char *ptr;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl; mib[] = CTL_NET;
mib[] = AF_ROUTE;
mib[] = ;
mib[] = AF_LINK;
mib[] = NET_RT_IFLIST; if ((mib[] = if_nametoindex("en0")) == ) {
printf("Error: if_nametoindex error\n");
return NULL;
} if (sysctl(mib, , NULL, &len, NULL, ) < ) {
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, , buf, &len, NULL, ) < ) {
printf("Error: sysctl, take 2");
free(buf);
return NULL;
} ifm = (struct if_msghdr *)buf;
sdl = (struct sockaddr_dl *)(ifm + );
ptr = (unsigned char *)LLADDR(sdl);
NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
*ptr, *(ptr+), *(ptr+), *(ptr+), *(ptr+), *(ptr+)];
free(buf); return outstring;
}

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

- (NSString *) stringFromMD5{

    if(self == nil || [self length] == )
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 * ];
for(NSInteger count = ; count < CC_MD5_DIGEST_LENGTH; count++){
[outputString appendFormat:@"%02x",outputBuffer[count]];
} return [outputString autorelease];
}

iOS开源项目:UIDevice-with-UniqueIdentifier-for-iOS-5的更多相关文章

  1. (转)直接拿来用!最火的iOS开源项目(二)

    “每一次的改变总意味着新的开始.”这句话用在iOS上可谓是再合适不过的了.GitHub上的iOS开源项目数不胜数,iOS每一次的改变,总会引发iOS开源项目的演变,从iOS 1.x到如今的iOS 7, ...

  2. GitHub上非常受开发者欢迎的iOS开源项目(二)

    "每一次的改变总意味着新的开始."这句话用在iOS上可谓是再合适不过的了.GitHub上的iOS开源项目数不胜数,iOS每一次的改变,总会引发iOS开源项目的演变,从iOS 1.x ...

  3. 直接拿来用!最火的iOS开源项目(一~三)

    结束了GitHub平台上“最受欢迎的Android开源项目”系列盘点之后,我们正式迎来了“GitHub上最受欢迎的iOS开源项目”系列盘点.今天,我们将介绍20个在GitHub上非常受开发者欢迎的iO ...

  4. 直接拿来用!最火的iOS开源项目(二)

    每一次的改变总意味着新的开始.”这句话用在iOS上可谓是再合适不过的了.GitHub上的iOS开源项目数不胜数,iOS每一次的改变,总会引发iOS开源项目的演变,从iOS 1.x到如今的iOS 7,有 ...

  5. iOS开源项目周报0105

    由OpenDigg 出品的iOS开源项目周报第四期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开发方面的开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等. He ...

  6. iOS开源项目周报1229

    由OpenDigg 出品的iOS开源项目周报第三期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开发方面的开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等. Ma ...

  7. iOS开源项目周报1222

    由OpenDigg 出品的iOS开源项目周报第二期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开发方面的开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等. io ...

  8. iOS开源项目周报1215

    由OpenDigg 出品的iOS开源项目周报第一期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开发方面的开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等. PY ...

  9. 直接拿来用!最火的iOS开源项目

    1. AFNetworking 在众多iOS开源项目中,AFNetworking可以称得上是最受开发者欢迎的库项目.AFNetworking是一个轻量级的iOS.Mac OS X网络通信类库,现在是G ...

  10. (转)直接拿来用!最火的iOS开源项目(一)

    1. AFNetworking 在众多iOS开源项目中,AFNetworking可以称得上是最受开发者欢迎的库项目.AFNetworking是一个轻量级的iOS.Mac OS X网络通信类库,现在是G ...

随机推荐

  1. CentOS/RHEL Linux安装EPEL第三方软件源

    https://www.vpser.net/manage/centos-rhel-linux-third-party-source-epel.html

  2. c++ primer 2 变量和基本类型

    2.1 基本内置类型 基本内置类型是C++“自带”的类型,区别于标准库定义的类型.使用时不需要应用标准库就可以使用,我们可以理解为数字型有下面这些 整形:就是整数或者是没有小数位的数.它包括bool( ...

  3. C# 6.0 新特性 (四)

    原文: 1.http://www.cnblogs.com/BoyceYang/p/3711343.html 2.http://www.cnblogs.com/lhking/p/3660182.html ...

  4. LoadRunner的Capture Level说明

    LoadRunner的Capture Level说明 Capture Level的设置说明: 1.Socket level data. Capture data using trapping on t ...

  5. LeetCode 136. Single Number(只出现一次的数字)

    LeetCode 136. Single Number(只出现一次的数字)

  6. HTML5 Canvas 初探

    仅仅只是一个简单的hello world. js代码很简单: <!DOCTYPE HTML> <html lang="cn"> <head> & ...

  7. linux 笔记(一)

    1.Linux 安装3ython3 1.1 下载 wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz 1.2 解压 tar -z ...

  8. thinkphp5.0安装

    ThinkPHP5的环境要求如下: PHP >= 5.4.0 PDO PHP Extension MBstring PHP Extension CURL PHP Extension 严格来说,T ...

  9. windows环境下模仿Linux环境发起curl请求

    1.到官网下载curl工具包 2.在curl.exe目录中使用(使用方式1) 解压下载后的压缩文件,通过cmd命令进入到curl.exe所在的目录. 由于博主使用的是windows 64位 的系统,因 ...

  10. Bzoj2510 弱题(矩阵快速幂)

    题面(权限题) 题解 一道概率\(dp\),可以设\(f[i][j]\)表示第\(i\)次操作后,标号为\(j\)的小球的期望个数,那么有: \[ \begin{aligned} &f[i][ ...