在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