在iOS的开发过程中,我们常常会遇到获取设备的UUID,IDFV,IDFA,MAC地址等一系列的设备标识符。
什么是设备的UUID?更详细的介绍可以参考www.cocoachina.com/industry/20130422/6040.html
代码如下:
-
获取广告标示符(IDFA-identifierForIdentifier)
导入 #import < AdSupport/ASIdentifierManager.h >
+(NSString *)dy_getDeviceIDFA{
ASIdentifierManager *asIM = [[ASIdentifierManager alloc] init];
NSString *idfaStr = [asIM.advertisingIdentifier UUIDString];
return idfaStr;
}
-
获取Vindor标示符 (IDFV-identifierForVendor)
+(NSString *)dy_getDeviceIDFV{
NSString* idfvStr = [[UIDevice currentDevice] identifierForVendor].UUIDString;
return idfvStr;
}
Git上的erica的UIDevice扩展文件,以前可用但由于IOKit framework没有公开,所以也无法使用。就算手动导入,依旧无法使用,看来获取IMEI要失败了,同时失败的还有IMSI。不过还存在另外一种可能,Stack Overflow上有人提供采用com.apple.coretelephony.Identity.get entitlement方法,but device must be jailbroken;在此附上链接,供大家参考:http://stackoverflow.com/questions/16667988/how-to-get-imei-on-iphone-5/16677043#16677043
-
获取MAC地址
include
include
include
include
+(NSString*)dy_getDeviceMAC{
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 macStr = [NSString stringWithFormat:@”%02X:%02X:%02X:%02X:%02X:%02X”,ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
free(buf);
return macStr;
}
-
获取UUID
+(NSString*)dy_getDeviceUUID{
CFUUIDRef uuid = CFUUIDCreate(NULL);
assert(uuid != NULL);
CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
return (__bridge NSString *)(uuidStr);
}
UDID是肯定要被苹果拒绝的,Stack Overflow提供了另外一种方法,越狱可尝试:http://stackoverflow.com/questions/27602368/how-to-get-serial-number-of-a-device-using-iokit-in-ios8-as-ioplatformserialnumb/27686125#27686125