UIDevice+IdentifierAddition.h:

#import <Foundation/Foundation.h>

@interface UIDevice (IdentifierAddition)

- (NSString *) uniqueDeviceIdentifier;

@end

UIDevice+IdentifierAddition.m

#import "UIDevice+IdentifierAddition.h"
#import "NSString+MD5.h"
#import "EnvConstant.h" #include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h> #define IS_IOS_7 ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0 ? YES : NO) @interface UIDevice(Private) - (NSString *) macaddress; @end @implementation UIDevice (IdentifierAddition) #pragma mark - Private Methods
// Return the local MAC addy
// Courtesy of FreeBSD hackers email list
// Accidentally munged during previous update. Fixed thanks to erica sadun & mlamb.
- (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;
} #pragma mark - uniqueDeviceIdentifier
- (NSString *) uniqueDeviceIdentifier{ if(IS_IOS_7){
NSString *identifierForVendor = [NSString stringWithFormat:@"%@",[[[UIDevice currentDevice]identifierForVendor]UUIDString]];
NSLog(@"identifierForVendor=%@",identifierForVendor);
NSString *stringToHash = [NSString stringWithFormat:@"%@%@",identifierForVendor,DEVICE_TOKEN_PASS];
NSLog(@"stringToHash = %@",stringToHash);
NSString *result = [NSString md5:stringToHash];
NSLog(@"result = %@",result);
return result;
}else{
NSString *macaddress = [[UIDevice currentDevice] macaddress];
NSLog(@"macaddress = %@",macaddress);
NSString *stringToHash = [NSString stringWithFormat:@"%@%@",macaddress,DEVICE_TOKEN_PASS];
NSLog(@"stringToHash = %@",stringToHash);
NSString *result = [NSString md5:stringToHash];
NSLog(@"result = %@",result);
return result;
}
} @end

NSString+MD5.h:

#import <Foundation/Foundation.h>

@interface NSString (MD5)

+(NSString *)md5:(NSString *)str;
@end

NSString+MD5.m:

#import "NSString+MD5.h"
#import <CommonCrypto/CommonDigest.h> // Need to import for CC_MD5 access @implementation NSString (MD5) +(NSString *)md5:(NSString *)str{
const char *cStr = [str UTF8String];
unsigned char result[16];
CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
return [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}
@end

用法:

[[UIDevice currentDevice]uniqueDeviceIdentifier];

【IOS工具类】获得设备唯一标识(兼容IOS5,6,7)的更多相关文章

  1. ios开发——实用技术篇OC篇&获取设备唯一标识

    获取设备唯一标识 WWDC 2013已经闭幕,IOS7 Beta随即发布,界面之难看无以言表...,简直就是山寨Android. 更让IOS程序猿悲催的是,设备唯一标识的MAC Address在IOS ...

  2. ios设备唯一标识获取策略

    In iOS 7 and later, if you ask for the MAC address of an iOS device, the system returns the value 02 ...

  3. iOS获取设备唯一标识的8种方法

    8种iOS获取设备唯一标识的方法,希望对大家有用. UDID UDID(Unique Device Identifier),iOS 设备的唯一识别码,是一个40位十六进制序列(越狱的设备通过某些工具可 ...

  4. [转]iOS设备唯一标识探讨

    转自:http://www.jianshu.com/p/b83b0240bd0e iOS设备唯一标识探讨 为了统计和检测应用的使用数据,几乎每家公司都有获取唯一标识的业务需求,在iOS5以前获取唯一标 ...

  5. iOS获取设备唯一标识的各种方法?IDFA、IDFV、UDID分别是什么含义?

    一.UDID (Unique Device Identifier) UDID的全称是Unique Device Identifier,顾名思义,它就是苹果IOS设备的唯一识别码,它由40个字符的字母和 ...

  6. iOS设备唯一标识的前世今生

    设备唯一标识 估计很多开发都有被要求过获取一下设备的唯一标识,获取设备的唯一标识经常使用在我们做统计或者是在保证一台设备登录亦或者是做IM的时候可能会考虑去使用它,这一次在自己的需求当中就有一个&qu ...

  7. 获取iOS设备唯一标识

    [获取iOS设备唯一标识] 1.已禁用-[UIDevice uniqueIdentifier] 苹果总是把用户的隐私看的很重要.-[UIDevice uniqueIdentifier]在iOS5实际在 ...

  8. 用keychain这个特点来保存设备唯一标识。

    由于IOS系统存储的数据都是在sandBox里面,一旦删除App,sandBox也不复存在.好在有一个例外,那就是keychain(钥匙串). 通常情况下,IOS系统用NSUserDefaults存储 ...

  9. IOS7如何获取设备唯一标识

    WWDC 2013已经闭幕,IOS7 Beta随即发布,界面之难看无以言表...,简直就是山寨Android. 更让IOS程序猿悲催的是,设备唯一标识的MAC Address在IOS7中也失效了. I ...

  10. 获取设备唯一标识 uuid(采用第三方库SSKeychain)

    SSKeyChain 下载链接: http://pan.baidu.com/s/1booV3VD 密码: ivdi /** *  获取设备唯一标识 uuid */ +(NSString*) uuid ...

随机推荐

  1. Net MVC轻量级分页控件

    JPager.Net MVC超好用轻量级分页控件   JPager.Net  MVC好用的轻量级分页控件,好用到你无法想象,轻量到你无法想象. JPager.Net  MVC好用的轻量级分页控件,实现 ...

  2. 如何使用springmvc的@requestbody 返回json数据

    先配置 XXX_ servletxml <!-- 整合jackson 返回一个json格式 --><bean class="org.springframework.web. ...

  3. Table中的JCheckBox TableHeader的全选(全反选)功能

    菜鸟学习ing class CheckDefaultModel extends DefaultTableModel /* * To change this template, choose Tools ...

  4. inner join、left join、right join中where和and的作用

    inner join.left join.right join中where和and的作用 .内连接(自然连接): 只有两个表相匹配的行才能在结果集中出现 2.外连接: 包括  (1)左外连接 (左边的 ...

  5. VB.net总结

    .NET视频差点儿相同用时一周结束,总体感觉就是"走耳不走脑".刚開始看视频理解起来有一点困难,台湾口音以及台湾与大陆计算机术语的差异,让我把前几集相当于直接忽略过了(建议打算看这 ...

  6. swap函數 进阶探讨与实现

    相信以下這個C程序非常多人都見過啦.當時自己看 美少女战士谭浩强 写的那本书上的解释.反正我当时是没看太懂详细是什么意思.谱架啊~~~ #include <stdio.h> void sw ...

  7. chrome你这是入侵OSX了么?

    今天突然发现安装chrome的应用后,会在Dock中出现如下图标,点击即可启动chrome相关应用,很是方便.

  8. 组件状态(TComponentState)11种和组件状态(TComponentStyle)4种

    TOperation = (opInsert, opRemove); TComponentState = set of ( csAncestor The component was introduce ...

  9. Oracle 的一张表没有主键,如何映射Hibernate

    我的一个Oracle表,没有任何主键,然后生成的时候就将所有的字段都作为联合主键,如果所有的字段都做联合主键的话,这样只要一个字段为null,查询的话这条记录就不能查询到. 然后我想到Oracle数据 ...

  10. hdu 3350

    hdu 3350 题意:让你求运算式的结果和运算过程中加法的次数 (a) > (b) ? (a) : (b) 大于取a,小于等于取b MAX( 1 + 2 , 3) 因为(a) > (b) ...