參考
Vendor Identification
UIDevice中的identifierForVendor方法是最接近替代uniqueIdentifier的另外一個(gè)方法,它返回NSUUID。在相同的設(shè)備中相同供應(yīng)商的app共享一個(gè)UUID。不同的供應(yīng)商在同一個(gè)設(shè)備上面將會(huì)返回不同的identifierForVendor值,就像相同供應(yīng)商在不同設(shè)備上面一樣。
對(duì)于開(kāi)發(fā)者來(lái)說(shuō),這個(gè)值提供了和原來(lái)相似的功能,而且沒(méi)有用戶(hù)隱私的問(wèn)題。
但是美中不足的是如果用卸載了供應(yīng)商下面所有的app,這個(gè)id就會(huì)被銷(xiāo)毀,重新安裝之后就會(huì)生成一個(gè)新的供應(yīng)商ID。
NSString *identifeir = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
Advertising Identification
ADSupprt模塊包含了ASIdentifierManager類(lèi),它有一個(gè)advertisingIdentifier方法。它返回一個(gè)NSUUID可以用來(lái)達(dá)到追蹤廣告的目的。
還有一個(gè)方法avertisingTrackingEnable.它返回一個(gè)BOOL類(lèi)型的數(shù)據(jù)用來(lái)指定是否用戶(hù)允許進(jìn)行廣告追蹤。
@import AdSupport;
// ...
NSString *identifier = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
NSString *isEnabled = [[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled] ? @"YES" : @"NO";
Network Identification
當(dāng) uniqueIdentifier 被棄用了,通過(guò)使用MAC地址變得很流行。一個(gè)MAC地址是恒定不變的且唯一的,可以用來(lái)跟蹤用戶(hù)。但是在iOS7中Apple對(duì)這個(gè)也添加了限制,所以實(shí)際返回的MAC地址為: 02:00:00:00:00:00. 關(guān)閉這個(gè)“漏洞”,將會(huì)推動(dòng)開(kāi)發(fā)者運(yùn)用Apple提供的優(yōu)先方法來(lái)進(jìn)行獲得設(shè)備標(biāo)識(shí).
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
//...
- (NSString *)getMacAddress {
int mgmtInfoBase[6];
char *msgBuffer = NULL;
NSString *errorFlag = NULL;
size_t length;
// Setup the management Information Base (mib)
mgmtInfoBase[0] = CTL_NET; // Request network subsystem
mgmtInfoBase[1] = AF_ROUTE; // Routing table info
mgmtInfoBase[2] = 0;
mgmtInfoBase[3] = AF_LINK; // Request link layer information
mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces
// With all configured interfaces requested, get handle index
if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)
errorFlag = @"if_nametoindex failure";
// Get the size of the data available (store in len)
else if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
errorFlag = @"sysctl mgmtInfoBase failure";
// Alloc memory based on above call
else if ((msgBuffer = malloc(length)) == NULL)
errorFlag = @"buffer allocation failure";
// Get system information, store in buffer
else if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
{
free(msgBuffer);
errorFlag = @"sysctl msgBuffer failure";
}
else
{
// Map msgbuffer to interface message structure
struct if_msghdr *interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
// Map to link-level socket structure
struct sockaddr_dl *socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);
// Copy link layer address data in socket structure to an array
unsigned char macAddress[6];
memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);
// Read from char array into a string object, into traditional Mac address format
NSString *macAddressString = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]];
// Release the buffer memory
free(msgBuffer);
return macAddressString;
}
return errorFlag;
}
關(guān)于 identity
關(guān)于 UUID (設(shè)備唯一標(biāo)識(shí)符)我之前也有研究過(guò),可以戳這里:
iOS—獲取設(shè)備型號(hào)和App版本號(hào)等信息
得到的最終方案是: UIDevice中的identifierForVendor方法配合 keychain(鑰匙串)存儲(chǔ)標(biāo)識(shí)符。