【IOS工具类】获得设备唯一标识(兼容IOS5,6,7)
来源:互联网 发布:淘宝旺铺功能 编辑:程序博客网 时间:2024/06/05 15:30
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];
1 0
- 【IOS工具类】获得设备唯一标识(兼容IOS5,6,7)
- 【IOS工具类】获得设备唯一标识,删除应用不重新生成
- ios 设备唯一标识
- Unity3d获得android和ios设备的唯一标识
- Unity3d获得android和ios设备的唯一标识
- Unity3D获得Android和iOS设备的唯一标识
- IOS获取设备唯一标识
- iOS设备唯一标识<二>
- 获取iOS设备唯一标识
- 获取iOS设备唯一标识
- iOS 获取设备唯一标识
- 获取iOS设备唯一标识
- iOS设备唯一标识探讨
- 获取iOS设备唯一标识
- iOS 获取设备唯一标识
- 获取iOS设备唯一标识
- iOS设备唯一标识保存
- iOS获取设备唯一标识
- 黑马程序员——Java基础知识总结(超级经典)
- PHP移动互联网开发笔记(3)——运算符
- Python装饰器学习(九步入门)
- ioctl,unlocked_ioctl 处理方法【转】
- C# 压缩、解压缩文件
- 【IOS工具类】获得设备唯一标识(兼容IOS5,6,7)
- VIM常用配置
- 用cmd编译运行JAVA的最基本操作
- 两台linux主机之间传送文件
- 最小二乘君根据样本数据推算直线方程系数
- java中的volatile关键字
- Linux NFS 和 Samba 共享配置
- Java与C++区别
- SQL Profile 总结(二)