天天品嘗iOS7甜點(diǎn) Day9:Device Identification

參考

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í)符。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 國(guó)家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說(shuō)閱讀 12,556評(píng)論 6 13
  • iOS6新特性 一、關(guān)于內(nèi)存警告 ios6中廢除了viewDidUnload,viewWillUnload這兩個(gè)系...
    Jimmy_P閱讀 2,423評(píng)論 3 29
  • ?逆時(shí)光? 人無(wú)法逆時(shí)找尋過(guò)去的美好 親人 愛(ài)人 朋友 擦肩 不可追 事業(yè) 機(jī)遇 愛(ài)好 選擇 不可求 這大概就是人...
    ALi躍嘉閱讀 206評(píng)論 0 0
  • 我,在死亡的路口,和很多很多人一起,排著隊(duì)拿著選擇的號(hào)碼牌,我龜縮在幾乎最后觀望。臨到終點(diǎn),閻羅問(wèn)我要去哪條路。...
    Chrisphr閱讀 333評(píng)論 0 0
  • 大學(xué)新生一個(gè)很明顯的特點(diǎn)就是迷茫。當(dāng)你沒(méi)有目標(biāo)沒(méi)有追求時(shí),日子就會(huì)變得貌似忙碌卻又著實(shí)空虛的樣子。按著課表循...
    丘山壑閱讀 226評(píng)論 0 1

友情鏈接更多精彩內(nèi)容