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 D Uring 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 *identifierFo Rvendor = [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); 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
Usage:
[[Uidevice Currentdevice]uniquedeviceidentifier];
iOS tools class obtains device unique identification (compatible with ios5,6,7)