【IOS工具类】获得设备唯一标识(兼容IOS5,6,7)
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)的更多相关文章
- ios开发——实用技术篇OC篇&获取设备唯一标识
获取设备唯一标识 WWDC 2013已经闭幕,IOS7 Beta随即发布,界面之难看无以言表...,简直就是山寨Android. 更让IOS程序猿悲催的是,设备唯一标识的MAC Address在IOS ...
- ios设备唯一标识获取策略
In iOS 7 and later, if you ask for the MAC address of an iOS device, the system returns the value 02 ...
- iOS获取设备唯一标识的8种方法
8种iOS获取设备唯一标识的方法,希望对大家有用. UDID UDID(Unique Device Identifier),iOS 设备的唯一识别码,是一个40位十六进制序列(越狱的设备通过某些工具可 ...
- [转]iOS设备唯一标识探讨
转自:http://www.jianshu.com/p/b83b0240bd0e iOS设备唯一标识探讨 为了统计和检测应用的使用数据,几乎每家公司都有获取唯一标识的业务需求,在iOS5以前获取唯一标 ...
- iOS获取设备唯一标识的各种方法?IDFA、IDFV、UDID分别是什么含义?
一.UDID (Unique Device Identifier) UDID的全称是Unique Device Identifier,顾名思义,它就是苹果IOS设备的唯一识别码,它由40个字符的字母和 ...
- iOS设备唯一标识的前世今生
设备唯一标识 估计很多开发都有被要求过获取一下设备的唯一标识,获取设备的唯一标识经常使用在我们做统计或者是在保证一台设备登录亦或者是做IM的时候可能会考虑去使用它,这一次在自己的需求当中就有一个&qu ...
- 获取iOS设备唯一标识
[获取iOS设备唯一标识] 1.已禁用-[UIDevice uniqueIdentifier] 苹果总是把用户的隐私看的很重要.-[UIDevice uniqueIdentifier]在iOS5实际在 ...
- 用keychain这个特点来保存设备唯一标识。
由于IOS系统存储的数据都是在sandBox里面,一旦删除App,sandBox也不复存在.好在有一个例外,那就是keychain(钥匙串). 通常情况下,IOS系统用NSUserDefaults存储 ...
- IOS7如何获取设备唯一标识
WWDC 2013已经闭幕,IOS7 Beta随即发布,界面之难看无以言表...,简直就是山寨Android. 更让IOS程序猿悲催的是,设备唯一标识的MAC Address在IOS7中也失效了. I ...
- 获取设备唯一标识 uuid(采用第三方库SSKeychain)
SSKeyChain 下载链接: http://pan.baidu.com/s/1booV3VD 密码: ivdi /** * 获取设备唯一标识 uuid */ +(NSString*) uuid ...
随机推荐
- zepto.js介绍(持续更新)
前言: zepto是一个简化版的jQuery,主要针对移动端开发. 简化了jQuery里很多的浏览器兼容性代码,jQuery的很多方法都被拿掉了(eg:slideUp). WP设备兼容性很差. 官方链 ...
- vtk基础编程(2)-读取数据文件中的坐标点
原文地址: http://blog.csdn.net/chinamming/article/details/16860051 1. 案例说明 在实际计算中,常常需要大量的数据, 这个时候数据文件就必不 ...
- System.gc
Java中的内存分配是随着new一个新的对象来实现的,这个很简单,而且也还是有一些可以“改进”内存回收的机制的,其中最显眼的就是这个System.gc()函数. 乍一看这个函数似乎是可以进行垃圾回收的 ...
- Eclipse验证码
package MyEclipse; import java.io.*; public class MyEclipseGen { private static final String ...
- bottle-session 0.3 : Python Package Index
bottle-session 0.3 : Python Package Index bottle-session 0.3
- unity3d游戏开发猜想——当程序猿老去
程序猿将代码注入生命去打造互联网的浪潮之巅.当有一天他们老了.会走向那里,会做些什么? 4.4.0" alt="" style="border:0px; ver ...
- [hadoop系列]Pig的安装和简单演示样例
inkfish原创,请勿商业性质转载,转载请注明来源(http://blog.csdn.net/inkfish ).(来源:http://blog.csdn.net/inkfish) Pig是Yaho ...
- swift 有些语法还是不支持。
<pre name="code" class="html">"func hasAnyMatches(list: Int[], condit ...
- Selenium 出现: Caused by: java.lang.ClassNotFoundException: org.w3c.dom.ElementTraversal
webDriver 运行的时候出现: Caused by: java.lang.ClassNotFoundException: org.w3c.dom.ElementTraversal 解决办法: 只 ...
- android设置背景图片透明
设置Activiyt为透明可以在Activity中引用系统透明主题android:theme="@android:style/Theme.Translucent" 设置背景图片透明 ...