UIDevice的uniqueIdentifier方法在ios7就不支持了, 为了获得设备相关唯一标识符, 
参考了这里:
https://github.com/Itayber/UIDevice-uniqueID

但是改了部分代码(下面会贴上代码). 另外,真机编译会出问题,解决记录如下:
1.  把我修改了的UIDevice-uniqueID.h/m(见下面代码)加到工程里.
2.  加IOKit.framework:
把IOKit.framework(在/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/System/Library/Frameworks/IOKit.framework) 拖到 编译选项-Build Phases-Link Binary With Libraries里.(注意到这个framework里没有头文件...)
.  加IOKit的头文件:
    在工程目录下的源文件目录里新建IOKit文件夹,
把/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.0.sdk/System/Library/Frameworks/IOKit.framework/Headers里的文件都拷贝到该目录里.
    同时添加好头文件搜索路径:
    在编译选项-Build Settings-Header Search Paths添加$SRCROOT,并且可递归遍历:recursive.
. done.

注意:这个方法虽然算出了设备相关的唯一标识符,但其结果和原uniqueIdentifier函数结果是不一样的,且如果上appStore很有可能会被苹果审核为不通过.
--------------------------------------------贴代码:--------------------------------------------
UIDevice-uniqueID.h

  1. /**
  2. * @brief 计算设备相关唯一标识符.
  3. * @note 来自https://github.com/Itayber/UIDevice-uniqueID,
  4. 但是修改了部分算法.by xiaoU.
  5. **/
  6.  
  7. #import <UIKit/UIDevice.h>
  8.  
  9. @interface UIDevice (uniqueID)
  10.  
  11. - (NSString *) uniqueID;
  12.  
  13. - (NSString *) wifiMAC;
  14. - (NSString *) bluetoothMAC;
  15. - (NSString *) serialNumber;
  16. - (NSString *) deviceIMEI;
  17. - (NSString *)deviceECID;
  18.  
  19. @end

 UIDevice-uniqueID.m

  1. #import "UIDevice-uniqueID.h"
  2. #include <arpa/inet.h>
  3. #include <net/if.h>
  4. #include <net/if_dl.h>
  5. #include <arpa/inet.h>
  6. #include <ifaddrs.h>
  7. #import <mach/mach_port.h>
  8. #import <CommonCrypto/CommonDigest.h>
  9.  
  10. #import <IOKit/IOKitLib.h> // add by U.
  11.  
  12. NSArray *getValue(NSString *iosearch);
  13.  
  14. // thanks Erica Sadun!
  15. // (spent time on this without realizing you had already wrote what I was looking for!)
  16. NSArray *getValue(NSString *iosearch)
  17. {
  18. mach_port_t masterPort;
  19. CFTypeID propID = (CFTypeID) NULL;
  20. unsigned int bufSize;
  21.  
  22. kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &masterPort);
  23. if (kr != noErr) return nil;
  24.  
  25. io_registry_entry_t entry = IORegistryGetRootEntry(masterPort);
  26. if (entry == MACH_PORT_NULL) return nil;
  27.  
  28. CFTypeRef prop = IORegistryEntrySearchCFProperty(entry, kIODeviceTreePlane, (CFStringRef) iosearch, nil, kIORegistryIterateRecursively);
  29. if (!prop) return nil;
  30.  
  31. propID = CFGetTypeID(prop);
  32. if (!(propID == CFDataGetTypeID()))
  33. {
  34. mach_port_deallocate(mach_task_self(), masterPort);
  35. return nil;
  36. }
  37.  
  38. CFDataRef propData = (CFDataRef) prop;
  39. if (!propData) return nil;
  40.  
  41. bufSize = CFDataGetLength(propData);
  42. if (!bufSize) return nil;
  43.  
  44. NSString *p1 = [[[NSString alloc] initWithBytes:CFDataGetBytePtr(propData) length:bufSize encoding:1] autorelease];
  45. mach_port_deallocate(mach_task_self(), masterPort);
  46. return [p1 componentsSeparatedByString:@"\0"];
  47. }
  48.  
  49. @implementation UIDevice (uniqueID)
  50.  
  51. // UDID = SHA1(SerialNumber + IMEI + WiFiAddress + BluetoothAddress)
  52. // http://iphonedevwiki.net/index.php/Lockdownd
  53. /** add by U: 这网址里说了, iphone4之后,公示应该是:SHA1(SerialNumber + ECID + WiFiAddress + BluetoothAddress).
  54. 而实际真机测试发现- deviceIMEI函数获取不到IMEI.
  55. 所以修改了这函数. */
  56. - (NSString *) uniqueID
  57. {
  58.  
  59. // Returns a random hash if run in the simulator
  60. #if TARGET_IPHONE_SIMULATOR
  61.  
  62. return [[[[[NSProcessInfo processInfo] globallyUniqueString] stringByReplacingOccurrencesOfString:@"-" withString:@""] substringToIndex:40] lowercaseString];
  63.  
  64. #endif
  65.  
  66. NSString *concat = [NSString stringWithFormat:@"%@%@%@%@",
  67. [self serialNumber],
  68. [self deviceECID],//[self deviceIMEI],
  69. [self wifiMAC],
  70. [self bluetoothMAC]];
  71.  
  72. const char *cconcat = [concat UTF8String];
  73.  
  74. unsigned char result[20];
  75. CC_SHA1(cconcat,strlen(cconcat),result);
  76.  
  77. NSMutableString *hash = [NSMutableString string];
  78. int i;
  79. for (i=0; i < 20; i++)
  80. {
  81. [hash appendFormat:@"%02x",result[i]];
  82. }
  83.  
  84. return [hash lowercaseString];
  85. }
  86.  
  87. - (NSString *) wifiMAC
  88. {
  89. struct ifaddrs *interfaces;
  90. const struct ifaddrs *tmpaddr;
  91.  
  92. if (getifaddrs(&interfaces)==0)
  93. {
  94. tmpaddr = interfaces;
  95.  
  96. while (tmpaddr != NULL)
  97. {
  98. if (strcmp(tmpaddr->ifa_name,"en0")==0)
  99. {
  100. struct sockaddr_dl *dl_addr = ((struct sockaddr_dl *)tmpaddr->ifa_addr);
  101. uint8_t *base = (uint8_t *)&dl_addr->sdl_data[dl_addr->sdl_nlen];
  102.  
  103. NSMutableString *s = [NSMutableString string];
  104.  
  105. int i;
  106.  
  107. for (i=0; i < dl_addr->sdl_alen; i++)
  108. {
  109. [s appendFormat:(i!=0)?@":%02x":@"%02x",base[i]];
  110. }
  111.  
  112. return s;
  113. }
  114.  
  115. tmpaddr = tmpaddr->ifa_next;
  116. }
  117.  
  118. freeifaddrs(interfaces);
  119. }
  120. return @"00:00:00:00:00:00";
  121.  
  122. }
  123.  
  124. // I hope someone will find a better way to do this
  125. - (NSString *) bluetoothMAC
  126. {
  127. mach_port_t port;
  128.  
  129. IOMasterPort(MACH_PORT_NULL,&port);
  130.  
  131. CFMutableDictionaryRef bt_dict = IOServiceNameMatching("bluetooth");
  132. mach_port_t btservice = IOServiceGetMatchingService(port, bt_dict);
  133.  
  134. CFDataRef bt_data = (CFDataRef)IORegistryEntrySearchCFProperty(btservice,"IODevicTree",(CFStringRef)@"local-mac-address", kCFAllocatorDefault, 1);
  135.  
  136. NSString *string = [((NSData *)bt_data) description];
  137.  
  138. string = [string stringByReplacingOccurrencesOfString:@"<" withString:@""];
  139. string = [string stringByReplacingOccurrencesOfString:@">" withString:@""];
  140. string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
  141.  
  142. NSMutableString *btAddr = [NSMutableString string];
  143.  
  144. int x=0;
  145. while (x<12)
  146. {
  147. x++;
  148. [btAddr appendFormat:((x!=12&&x%2==0)?@"%C:":@"%C"),[string characterAtIndex:(x-1)]];
  149. }
  150.  
  151. return btAddr;
  152. }
  153.  
  154. - (NSString *) serialNumber
  155. {
  156. return [getValue(@"serial-number") objectAtIndex:0];
  157. }
  158.  
  159. - (NSString *) deviceIMEI
  160. {
  161. return [getValue(@"device-imei") objectAtIndex:0];
  162. }
  163.  
  164. /// add by U:
  165. //
  166. - (NSString *)deviceECID
  167. {
  168. NSString * res = nil;
  169. if (CFMutableDictionaryRef dict = IOServiceMatching("IOPlatformExpertDevice")) {
  170. if (io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, dict)) {
  171. if (CFTypeRef ecid = IORegistryEntrySearchCFProperty(service, kIODeviceTreePlane, CFSTR("unique-chip-id"), kCFAllocatorDefault, kIORegistryIterateRecursively)) {
  172. NSData *data((NSData *) ecid);
  173. size_t length([data length]);
  174. uint8_t bytes[length];
  175. [data getBytes:bytes];
  176. char string[length * 2 + 1];
  177. for (size_t i(0); i != length; ++i)
  178. sprintf(string + i * 2, "%.2X", bytes[length - i - 1]);
  179. printf("%s", string);
  180. res = [[[NSString alloc] initWithCString:string encoding:NSASCIIStringEncoding] autorelease];
  181. CFRelease(ecid);
  182. }
  183. IOObjectRelease(service);
  184. }
  185. }
  186. return res;
  187. }
  188.  
  189. @end

  

uniqueIdentifier在ios7不支持后的替代方法的更多相关文章

  1. 让IE浏览器支持CSS3圆角的方法

    如果要想在IE浏览器中实现圆角的效果,我们一般都会采用圆角图片的方式.用图片的话,基本就跟浏览器没有多大关系了,因为任何浏览器都支持这种方式.今天我们主要是讲解如果用CSS3样式表来实现圆角效果,值得 ...

  2. 让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法--(转)

    如有雷同,不胜荣幸,若转载,请注明 让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法 最近做一个Web网站,之前一直觉得bootstrap非常好,这次使用了boot ...

  3. SCRIPT438: 对象不支持“indexOf”属性或方法

    SCRIPT438: 对象不支持“indexOf”属性或方法 indexOf()的用法:返回字符中indexof(string)中字串string在父串中首次出现的位置,从0开始!没有返回-1:方便判 ...

  4. 不支持find_element_by_name元素定位方法,抛不支持find_element_by_name元素定位方法,会抛如下错误 org.openqa.selenium.InvalidSelectorException: Locator Strategy 'name' is not supported for this session的解决

    appium1.5后不支持find_element_by_name元素定位方法,会抛如下错误 org.openqa.selenium.InvalidSelectorException: Locator ...

  5. bootstrap的datepicker在选择日期后调用某个方法

    bootstrap的datepicker在选择日期后调用某个方法 2016-11-08 15:14 1311人阅读 评论(0) 收藏 举报 首先感谢网易LOFTER博主Ivy的博客,我才顿悟了问题所在 ...

  6. Lodop“对象不支持SET__LICENSES属性或方法”SET__LICENSES is not a function”

    Lodop中的方法如果书写错误,就会报错:“对象不支持XXX属性或方法”调试JS会报错”SET__LICENSES is not a function” LODOP.SET_LICENSES是加注册语 ...

  7. jQuery 报错,对象不支持tolowercase属性或方法

    泪流满面.<input>里id和name都不能是nodeName,否则跟jquery.js冲突 JQuery 实践问题 - toLowerCase 错误 在应用JQuery+easyui开 ...

  8. 转载------让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法

    本文是转载及收藏 让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法 最近做一个Web网站,之前一直觉得bootstrap非常好,这次使用了bootstrap3,在c ...

  9. jquery autocomplete s.toLowerCase(); 对象不支持此属性或方法

    今天发现了一个问题,自动提示删掉后再输入,会出现 s.toLowerCase(); 对象不支持此属性或方法的错误,后来格式化了jquery的autocomplete发现他是在matchSubset方法 ...

随机推荐

  1. 各种样式的table 及 代码

    1.模板一 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...

  2. 检测和删除多余无用的css

    本文主要讲解如何检测页面中多余无用的css. 1.chrome浏览器 F12审查元素的Audits 说明:使用Audits,会检测出页面中没有用到的css,需要手动删除多余的css:同时需要说明的是检 ...

  3. eclipes设置默认注释作者

    打开Perfences首选项页,搜索Template,找到Java>> Code Style >> Code Template功能,在打开的树中,找到Comments.Type ...

  4. sqlite 附加和分离数据库

    附加数据库 ATTACH DATABASE 'testDB.db' as 'TEST'; 分离数据库 DETACH DATABASE 'Test';

  5. mariadb多实例搭建

    测试环境基于centos7.2,腾讯云实验室,学习搭建! https://www.qcloud.com/developer 多实例mysql,能更加理解mysql安装的基本过程!及简单使用... ma ...

  6. 在交叉编译中使用最新版的SS

    因为旧版本的ss-local总是出现 shake hands failed 错误, 打算用最新的版本试试, 所以尝试在编译中使用最新版的shadowsocks. 项目地址 Shadowsocks-li ...

  7. 使用c:forEach 控制5个换行

    今天做项目的时候碰到一个问题,我须要显示不确定数目的图片在网页上(图片是从数据库查出来的),用的是<c:forEach>循环取值的.就须要做成一行显示固定个数的图片.代码例如以下(我这里是 ...

  8. 【laravel5.4 + TP5.0】hasOne和belongsTo的区别

    1.从字面理解:假如A比B大,那么A hasOne B: B belongsTo A: 2.个人总结: 3.从代码角度: 主要是看你是在哪一个model(模型)中编写这个关联关系,父关联对象就是在父关 ...

  9. SQL Server里面导出SQL脚本(表数据的insert语句)

    转载自:http://hi.baidu.com/pigarmy/blog/item/109894c445eab0a28326ac5a.html 最近需要导出一个表的数据并生成insert语句,发现SQ ...

  10. epoll 系列函数简介、与select、poll 的区别

    一.epoll 系列函数简介 #include <sys/epoll.h> int epoll_create(int size); int epoll_create1(int flags) ...