uniqueIdentifier在ios7不支持后的替代方法
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
- /**
- * @brief 计算设备相关唯一标识符.
- * @note 来自https://github.com/Itayber/UIDevice-uniqueID,
- 但是修改了部分算法.by xiaoU.
- **/
- #import <UIKit/UIDevice.h>
- @interface UIDevice (uniqueID)
- - (NSString *) uniqueID;
- - (NSString *) wifiMAC;
- - (NSString *) bluetoothMAC;
- - (NSString *) serialNumber;
- - (NSString *) deviceIMEI;
- - (NSString *)deviceECID;
- @end
UIDevice-uniqueID.m
- #import "UIDevice-uniqueID.h"
- #include <arpa/inet.h>
- #include <net/if.h>
- #include <net/if_dl.h>
- #include <arpa/inet.h>
- #include <ifaddrs.h>
- #import <mach/mach_port.h>
- #import <CommonCrypto/CommonDigest.h>
- #import <IOKit/IOKitLib.h> // add by U.
- NSArray *getValue(NSString *iosearch);
- // thanks Erica Sadun!
- // (spent time on this without realizing you had already wrote what I was looking for!)
- NSArray *getValue(NSString *iosearch)
- {
- mach_port_t masterPort;
- CFTypeID propID = (CFTypeID) NULL;
- unsigned int bufSize;
- kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &masterPort);
- if (kr != noErr) return nil;
- io_registry_entry_t entry = IORegistryGetRootEntry(masterPort);
- if (entry == MACH_PORT_NULL) return nil;
- CFTypeRef prop = IORegistryEntrySearchCFProperty(entry, kIODeviceTreePlane, (CFStringRef) iosearch, nil, kIORegistryIterateRecursively);
- if (!prop) return nil;
- propID = CFGetTypeID(prop);
- if (!(propID == CFDataGetTypeID()))
- {
- mach_port_deallocate(mach_task_self(), masterPort);
- return nil;
- }
- CFDataRef propData = (CFDataRef) prop;
- if (!propData) return nil;
- bufSize = CFDataGetLength(propData);
- if (!bufSize) return nil;
- NSString *p1 = [[[NSString alloc] initWithBytes:CFDataGetBytePtr(propData) length:bufSize encoding:1] autorelease];
- mach_port_deallocate(mach_task_self(), masterPort);
- return [p1 componentsSeparatedByString:@"\0"];
- }
- @implementation UIDevice (uniqueID)
- // UDID = SHA1(SerialNumber + IMEI + WiFiAddress + BluetoothAddress)
- // http://iphonedevwiki.net/index.php/Lockdownd
- /** add by U: 这网址里说了, iphone4之后,公示应该是:SHA1(SerialNumber + ECID + WiFiAddress + BluetoothAddress).
- 而实际真机测试发现- deviceIMEI函数获取不到IMEI.
- 所以修改了这函数. */
- - (NSString *) uniqueID
- {
- // Returns a random hash if run in the simulator
- #if TARGET_IPHONE_SIMULATOR
- return [[[[[NSProcessInfo processInfo] globallyUniqueString] stringByReplacingOccurrencesOfString:@"-" withString:@""] substringToIndex:40] lowercaseString];
- #endif
- NSString *concat = [NSString stringWithFormat:@"%@%@%@%@",
- [self serialNumber],
- [self deviceECID],//[self deviceIMEI],
- [self wifiMAC],
- [self bluetoothMAC]];
- const char *cconcat = [concat UTF8String];
- unsigned char result[20];
- CC_SHA1(cconcat,strlen(cconcat),result);
- NSMutableString *hash = [NSMutableString string];
- int i;
- for (i=0; i < 20; i++)
- {
- [hash appendFormat:@"%02x",result[i]];
- }
- return [hash lowercaseString];
- }
- - (NSString *) wifiMAC
- {
- struct ifaddrs *interfaces;
- const struct ifaddrs *tmpaddr;
- if (getifaddrs(&interfaces)==0)
- {
- tmpaddr = interfaces;
- while (tmpaddr != NULL)
- {
- if (strcmp(tmpaddr->ifa_name,"en0")==0)
- {
- struct sockaddr_dl *dl_addr = ((struct sockaddr_dl *)tmpaddr->ifa_addr);
- uint8_t *base = (uint8_t *)&dl_addr->sdl_data[dl_addr->sdl_nlen];
- NSMutableString *s = [NSMutableString string];
- int i;
- for (i=0; i < dl_addr->sdl_alen; i++)
- {
- [s appendFormat:(i!=0)?@":%02x":@"%02x",base[i]];
- }
- return s;
- }
- tmpaddr = tmpaddr->ifa_next;
- }
- freeifaddrs(interfaces);
- }
- return @"00:00:00:00:00:00";
- }
- // I hope someone will find a better way to do this
- - (NSString *) bluetoothMAC
- {
- mach_port_t port;
- IOMasterPort(MACH_PORT_NULL,&port);
- CFMutableDictionaryRef bt_dict = IOServiceNameMatching("bluetooth");
- mach_port_t btservice = IOServiceGetMatchingService(port, bt_dict);
- CFDataRef bt_data = (CFDataRef)IORegistryEntrySearchCFProperty(btservice,"IODevicTree",(CFStringRef)@"local-mac-address", kCFAllocatorDefault, 1);
- NSString *string = [((NSData *)bt_data) description];
- string = [string stringByReplacingOccurrencesOfString:@"<" withString:@""];
- string = [string stringByReplacingOccurrencesOfString:@">" withString:@""];
- string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
- NSMutableString *btAddr = [NSMutableString string];
- int x=0;
- while (x<12)
- {
- x++;
- [btAddr appendFormat:((x!=12&&x%2==0)?@"%C:":@"%C"),[string characterAtIndex:(x-1)]];
- }
- return btAddr;
- }
- - (NSString *) serialNumber
- {
- return [getValue(@"serial-number") objectAtIndex:0];
- }
- - (NSString *) deviceIMEI
- {
- return [getValue(@"device-imei") objectAtIndex:0];
- }
- /// add by U:
- //
- - (NSString *)deviceECID
- {
- NSString * res = nil;
- if (CFMutableDictionaryRef dict = IOServiceMatching("IOPlatformExpertDevice")) {
- if (io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, dict)) {
- if (CFTypeRef ecid = IORegistryEntrySearchCFProperty(service, kIODeviceTreePlane, CFSTR("unique-chip-id"), kCFAllocatorDefault, kIORegistryIterateRecursively)) {
- NSData *data((NSData *) ecid);
- size_t length([data length]);
- uint8_t bytes[length];
- [data getBytes:bytes];
- char string[length * 2 + 1];
- for (size_t i(0); i != length; ++i)
- sprintf(string + i * 2, "%.2X", bytes[length - i - 1]);
- printf("%s", string);
- res = [[[NSString alloc] initWithCString:string encoding:NSASCIIStringEncoding] autorelease];
- CFRelease(ecid);
- }
- IOObjectRelease(service);
- }
- }
- return res;
- }
- @end
uniqueIdentifier在ios7不支持后的替代方法的更多相关文章
- 让IE浏览器支持CSS3圆角的方法
如果要想在IE浏览器中实现圆角的效果,我们一般都会采用圆角图片的方式.用图片的话,基本就跟浏览器没有多大关系了,因为任何浏览器都支持这种方式.今天我们主要是讲解如果用CSS3样式表来实现圆角效果,值得 ...
- 让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法--(转)
如有雷同,不胜荣幸,若转载,请注明 让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法 最近做一个Web网站,之前一直觉得bootstrap非常好,这次使用了boot ...
- SCRIPT438: 对象不支持“indexOf”属性或方法
SCRIPT438: 对象不支持“indexOf”属性或方法 indexOf()的用法:返回字符中indexof(string)中字串string在父串中首次出现的位置,从0开始!没有返回-1:方便判 ...
- 不支持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 ...
- bootstrap的datepicker在选择日期后调用某个方法
bootstrap的datepicker在选择日期后调用某个方法 2016-11-08 15:14 1311人阅读 评论(0) 收藏 举报 首先感谢网易LOFTER博主Ivy的博客,我才顿悟了问题所在 ...
- Lodop“对象不支持SET__LICENSES属性或方法”SET__LICENSES is not a function”
Lodop中的方法如果书写错误,就会报错:“对象不支持XXX属性或方法”调试JS会报错”SET__LICENSES is not a function” LODOP.SET_LICENSES是加注册语 ...
- jQuery 报错,对象不支持tolowercase属性或方法
泪流满面.<input>里id和name都不能是nodeName,否则跟jquery.js冲突 JQuery 实践问题 - toLowerCase 错误 在应用JQuery+easyui开 ...
- 转载------让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法
本文是转载及收藏 让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法 最近做一个Web网站,之前一直觉得bootstrap非常好,这次使用了bootstrap3,在c ...
- jquery autocomplete s.toLowerCase(); 对象不支持此属性或方法
今天发现了一个问题,自动提示删掉后再输入,会出现 s.toLowerCase(); 对象不支持此属性或方法的错误,后来格式化了jquery的autocomplete发现他是在matchSubset方法 ...
随机推荐
- 各种样式的table 及 代码
1.模板一 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...
- 检测和删除多余无用的css
本文主要讲解如何检测页面中多余无用的css. 1.chrome浏览器 F12审查元素的Audits 说明:使用Audits,会检测出页面中没有用到的css,需要手动删除多余的css:同时需要说明的是检 ...
- eclipes设置默认注释作者
打开Perfences首选项页,搜索Template,找到Java>> Code Style >> Code Template功能,在打开的树中,找到Comments.Type ...
- sqlite 附加和分离数据库
附加数据库 ATTACH DATABASE 'testDB.db' as 'TEST'; 分离数据库 DETACH DATABASE 'Test';
- mariadb多实例搭建
测试环境基于centos7.2,腾讯云实验室,学习搭建! https://www.qcloud.com/developer 多实例mysql,能更加理解mysql安装的基本过程!及简单使用... ma ...
- 在交叉编译中使用最新版的SS
因为旧版本的ss-local总是出现 shake hands failed 错误, 打算用最新的版本试试, 所以尝试在编译中使用最新版的shadowsocks. 项目地址 Shadowsocks-li ...
- 使用c:forEach 控制5个换行
今天做项目的时候碰到一个问题,我须要显示不确定数目的图片在网页上(图片是从数据库查出来的),用的是<c:forEach>循环取值的.就须要做成一行显示固定个数的图片.代码例如以下(我这里是 ...
- 【laravel5.4 + TP5.0】hasOne和belongsTo的区别
1.从字面理解:假如A比B大,那么A hasOne B: B belongsTo A: 2.个人总结: 3.从代码角度: 主要是看你是在哪一个model(模型)中编写这个关联关系,父关联对象就是在父关 ...
- SQL Server里面导出SQL脚本(表数据的insert语句)
转载自:http://hi.baidu.com/pigarmy/blog/item/109894c445eab0a28326ac5a.html 最近需要导出一个表的数据并生成insert语句,发现SQ ...
- epoll 系列函数简介、与select、poll 的区别
一.epoll 系列函数简介 #include <sys/epoll.h> int epoll_create(int size); int epoll_create1(int flags) ...