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方法 ...
随机推荐
- POJ 1579 Function Run Fun 记忆化递归
典型的记忆化递归问题. 这类问题的记忆主要是利用数组记忆.那么已经计算过的值就能够直接返回.不须要进一步递归了. 注意:下标越界.递归顺序不能错,及时推断是否已经计算过值了,不要多递归. 或者直接使用 ...
- apacheBench对网站进行压力测试
apacheBench对网站进行压力测试 分类: 学习 2014-02-19 10:35 4154人阅读 评论(1) 收藏 举报 apacheBench压力测试 Apache Benchmark下载 ...
- HeidiSQL数据库mysql/sql-server连接工具
HeidiSQL,是一款可以显示表在存储中占得空间,体积小的mysql.sql-server连接工具! 下载地址: https://www.heidisql.com/download.php 中文版: ...
- 微信小程序基于scroll-view实现锚点定位
代码地址如下:http://www.demodashi.com/demo/14009.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
- aop注解 事例
spring.xml中aop的配置 <!-- aop的配置 --> <aop:config> <!-- 切入点表达式 --> <aop:pointcut ex ...
- Android学习系列(4)--App自适应draw9patch不失真背景
做人要大度,海纳百川,做事要圆滑,左右逢源,这让我想到了编程也是如此,代码要扩展,界面也要考虑自适应.这篇文章是Android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用. 1. ...
- if you are not making someone else's life better, then you are wasting your time.– Will Smith如果你不能给别人的生活带来改善,那么你就是在浪费你的宝贵时间。 --威尔 史密斯(程序员,你做的东西...)
if you are not making someone else's life better, then you are wasting your time. – Will Smith 如果你不能 ...
- Python练习笔记——采用生成器函数实现两数之间的素数计算
题目:编写一个生成器函数myprimes(start, end),实现[start, end)范围内的所有素数计算2 3 5 7. ... 第一 常规函数方法 方法1 def myprime(num) ...
- GL_总账会计科目追寻SLA及子模组
相信做总账的学友们,一般很多时间都会花费在查询日记账的来源,因为R12多了一个SLA模组,又有些增加了追溯日记账的难度,个人整理了一下 11i过账方式: 子模组-> 总账 (Post Journ ...
- spring 多线程
http://blog.csdn.net/chszs/article/details/8219189 一.ThreadPoolTaskExecutor ThreadPoolTaskExecutor的配 ...