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. WPF中控件ListView和DataGrid典型属性介绍

    ListView GridView View视图 重要属性: public bool AllowsColumnReorder 获取或设置一个值,该值指示 System.Windows.Controls ...

  2. HDU 2896 病毒侵袭 AC自己主动机题解

    本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...

  3. 集合简单总结 ArrayList、List、Hashtable、Dictionary

      ============================ 集合综述 ============================== 1.什么是泛型: 泛型就是限制了操作类型,意思如下:       ...

  4. UML02-用例图

    1.泛化表示一般和特殊的关系.用例之间存在泛化关系,参与者之间存在泛化关系,参与者和用例之间存在泛化关系. 2.画出用例图. 系统允许管理员通过磁盘加载存货数据来运行存货清单报告: 管理员通过从磁盘加 ...

  5. Ultra Office Control 2.0

    http://www.ultrashareware.com/Ultra-Office-Control.htm

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

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

  7. Codeforces Round #112 (Div. 2)---A. Supercentral Point

    Supercentral Point time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. semaphore实现浏览器的读写原理

    在编程范式中的斯坦福大学的老师说了一个例子:好比世界上就只有一台互联网的服务器,当我们浏览网页的时候,就好比服务器进行了写操作,而浏览器则进行了读操作. 我如果用简单的伪代码c++写出来是这个样子的: ...

  9. 如何解决android logcat不打印信息在android开发中

    用eclipse进行android开发中经常遇到logcat无任何信息输出,这给我们调试程序带来很大的不便.解决办法:window-->show view-->选择android下的dev ...

  10. ASP.NET - 对URL传递的值进行编码Server.UrlEncode()

    /// <summary> /// 搜索内容 /// </summary> /// <param name="sender"></para ...