在開發(fā)過程中,我們經(jīng)常會被要求獲取每個設(shè)備的唯一標(biāo)示,以便后臺做相應(yīng)的處理。我們來看看有哪些方法來獲取設(shè)備的唯一標(biāo)示,然后再分析下這些方法的利弊。
具體可以分為如下幾種:
- UDID
- IDFA
- IDFV
- MAC
- keychain
下面我們來具體分析下每種獲取方法的利弊
1、UDID
什么是UDID
UDID 「Unique Device Identifier Description」是由子母和數(shù)字組成的40個字符串的序號,用來區(qū)別每一個唯一的iOS設(shè)備,包括 iPhones, iPads, 以及 iPod touches,這些編碼看起來是隨機的,實際上是跟硬件設(shè)備特點相聯(lián)系的,另外你可以到iTunes,pp助手或itools等軟件查看你的udid(設(shè)備標(biāo)識)
如下圖所示:

UDID是用來干什么的?
UDID可以關(guān)聯(lián)其它各種數(shù)據(jù)到相關(guān)設(shè)備上。例如,連接到開發(fā)者賬號,可以允許在發(fā)布前讓設(shè)備安裝或測試應(yīng)用;也可以讓開發(fā)者獲得iOS測試版進行體驗。蘋果用UDID連接到蘋果的ID,這些設(shè)備可以自動下載和安裝從App Store購買的應(yīng)用、保存從iTunes購買的音樂、幫助蘋果發(fā)送推送通知、即時消息。 在iOS 應(yīng)用早期,UDID被第三方應(yīng)用開發(fā)者和網(wǎng)絡(luò)廣告商用來收集用戶數(shù)據(jù),可以用來關(guān)聯(lián)地址、記錄應(yīng)用使用習(xí)慣……以便推送精準(zhǔn)廣告。
為什么蘋果反對開發(fā)人員使用UDID?
iOS 2.0版本以后UIDevice提供一個獲取設(shè)備唯一標(biāo)識符的方法uniqueIdentifier,通過該方法我們可以獲取設(shè)備的序列號,這個也是目前為止唯一可以確認(rèn)唯一的標(biāo)示符。 許多開發(fā)者把UDID跟用戶的真實姓名、密碼、住址、其它數(shù)據(jù)關(guān)聯(lián)起來;網(wǎng)絡(luò)窺探者會從多個應(yīng)用收集這些數(shù)據(jù),然后順藤摸瓜得到這個人的許多隱私數(shù)據(jù)。同時大部分應(yīng)用確實在頻繁傳輸UDID和私人信息。 為了避免集體訴訟,蘋果最終決定在iOS 5 的時候,將這一慣例廢除,開發(fā)者被引導(dǎo)生成一個唯一的標(biāo)識符,只能檢測應(yīng)用程序,其他的信息不提供。現(xiàn)在應(yīng)用試圖獲取UDID已被禁止且不允許上架。
所以這個方法作廢
2、IDFA
全名:
AdvertisingIdentifier
獲取代碼:
import AdSupport
var adId = ASIdentifierManager.shared.advertisingIdentifier.uuidString
- 來源:iOS6.0及以后
- 說明:直譯就是廣告id, 在同一個設(shè)備上的所有App都會取到相同的值,是蘋果專門給各廣告提供商用來追蹤用戶而設(shè)的,用戶可以在 設(shè)置|隱私|廣告追蹤 里重置此id的值,或限制此id的使用,故此id有可能會取不到值,但好在Apple默認(rèn)是允許追蹤的,而且一般用戶都不知道有這么個設(shè)置,所以基本上用來監(jiān)測推廣效果,是戳戳有余了。
- 注意:由于idfa會出現(xiàn)取不到的情況,故絕不可以作為業(yè)務(wù)分析的主id,來識別用戶。
3、IDFV
全名
IdentifierForVendor
獲取代碼
var idfv = UIDevice.current.identifierForVendor.uuidString
來源
iOS6.0及以后
說明
顧名思義,是給Vendor標(biāo)識用戶用的,每個設(shè)備在所屬同一個Vender的應(yīng)用里,都有相同的值。其中的Vender是指應(yīng)用提供商,但準(zhǔn)確點說,是通過BundleID的反轉(zhuǎn)的前兩部分進行匹配,如果相同就是同一個Vender,例如對于com.taobao.app1, com.taobao.app2 這兩個BundleID來說,就屬于同一個Vender,共享同一個idfv的值。和idfa不同的是,idfv的值是一定能取到的,所以非常適合于作為內(nèi)部用戶行為分析的主id,來標(biāo)識用戶,替代OpenUDID。
注意
如果用戶將屬于此Vender的所有App卸載,則idfv的值會被重置,即再重裝此Vender的App,idfv的值和之前不同。
4、MAC地址
使用WiFi的mac地址來取代已經(jīng)廢棄了的uniqueIdentifier方法。具體可見:
http://stackoverflow.com/questions/677530/how-can-i-programmatically-get-the-mac-address-of-an-iphone
然而在iOS 7中蘋果再一次無情的封殺mac地址,使用之前的方法獲取到的mac地址全部都變成了02:00:00:00:00:00。
5、Keychain
我們可以獲取到UUID,然后把UUID保存到KeyChain里面。
這樣以后即使APP刪了再裝回來,也可以從KeyChain中讀取回來。使用group還可以可以保證同一個開發(fā)商的所有程序針對同一臺設(shè)備能夠獲取到相同的不變的UDID。
但是刷機或重裝系統(tǒng)后uuid還是會改變。
把下面兩個類文件放到你的項目中
KeychainItemWrapper.swift文件
import UIKit
class KeychainItemWrapper: NSObject {
// The actual keychain item data backing store.
var keychainItemData = [AnyHashable: Any]()
var genericPasswordQuery = [AnyHashable: Any]()
// Designated initializer.
override init(account: String, service: String, accessGroup: String) {
}
override init(identifier: String, accessGroup: String) {
}
override func setObject(_ inObject: Any, forKey key: Any) {
}
override func object(forKey key: Any) -> Any {
}
// Initializes and resets the default generic keychain item data.
func resetKeychainItem() {
}
}
KeychainItemWrapper.swift文件
// The output below is limited by 4 KB.
// Upgrade your plan to remove this limitation.
import Security
/*
These are the default constants and their respective types,
available for the kSecClassGenericPassword Keychain Item class:
kSecAttrAccessGroup - CFStringRef
kSecAttrCreationDate - CFDateRef
kSecAttrModificationDate - CFDateRef
kSecAttrDescription - CFStringRef
kSecAttrComment - CFStringRef
kSecAttrCreator - CFNumberRef
kSecAttrType - CFNumberRef
kSecAttrLabel - CFStringRef
kSecAttrIsInvisible - CFBooleanRef
kSecAttrIsNegative - CFBooleanRef
kSecAttrAccount - CFStringRef
kSecAttrService - CFStringRef
kSecAttrGeneric - CFDataRef
See the header file Security/SecItem.h for more details.
*/
extension KeychainItemWrapper {
/*
The decision behind the following two methods (secItemFormatToDictionary and dictionaryToSecItemFormat) was
to encapsulate the transition between what the detail view controller was expecting (NSString *) and what the
Keychain API expects as a validly constructed container class.
*/
func secItemFormat(toDictionary dictionaryToConvert: [AnyHashable: Any]) -> [AnyHashable: Any] {
}
func dictionary(toSecItemFormat dictionaryToConvert: [AnyHashable: Any]) -> [AnyHashable: Any] {
}
// Updates the item in the keychain, or adds it if it doesn't exist.
func writeToKeychain() {
}
}
class KeychainItemWrapper {
override init(account: String, service: String, accessGroup: String) {
super.init()
assert(account != nil || service != nil, "Both account and service are nil. Must specifiy at least one.")
// Begin Keychain search setup. The genericPasswordQuery the attributes kSecAttrAccount and
// kSecAttrService are used as unique identifiers differentiating keychain items from one another
genericPasswordQuery = [AnyHashable: Any]()
genericPasswordQuery[(kSecClass as! Any)] = (kSecClassGenericPassword as! Any)
genericPasswordQuery[(kSecAttrAccount as! Any)] = account
genericPasswordQuery[(kSecAttrService as! Any)] = service
// The keychain access group attribute determines if this item can be shared
// amongst multiple apps whose code signing entitlements contain the same keychain access group.
if accessGroup != nil {
#if TARGET_IPHONE_SIMULATOR
// Ignore the access group if running on the iPhone simulator.
//
// Apps that are built for the simulator aren't signed, so there's no keychain access group
// for the simulator to check. This means that all apps can see all keychain items when run
// on the simulator.
//
// If a SecItem contains an access group attribute, SecItemAdd and SecItemUpdate on the
// simulator will return -25243 (errSecNoAccessForItem).
#else
genericPasswordQuery[(kSecAttrAccessGroup as! Any)] = accessGroup
#endif
}
// Use the proper search constants, return only the attributes of the first match.
genericPasswordQuery[(kSecMatchLimit as! Any)] = (kSecMatchLimitOne as! Any)
genericPasswordQuery[(kSecReturnAttributes as! Any)] = (kCFBooleanTrue as! Any)
var tempQuery = genericPasswordQuery
var outDictionary: [AnyHashable: Any]? = nil
if !SecItemCopyMatching((tempQuery as! CFDictionaryRef), (outDictionary as! CFTypeRef)) == noErr {
// Stick these default values into keychain item if nothing found.
self.resetKeychainItem()
//Adding the account and service identifiers to the keychain
keychainItemData[(kSecAttrAccount as! Any)] = account
keychainItemData[(kSecAttrService as! Any)] = service
if accessGroup != nil {
#if TARGET_IPHONE_SIMULATOR
// Ignore the access group if running on the iPhone simulator.
我們在寫一個工具類用來保存UUID到keychain和從keychain中讀取UUID.
實現(xiàn)代碼
AppUntils.m文件
import Security
// MARK: - 保存和讀取UUID
class func saveUUIDToKeyChain() {
var keychainItem = KeychainItemWrapper(account: "Identfier", service: "AppName", accessGroup: nil)
var string = (keychainItem[(kSecAttrGeneric as! Any)] as! String)
if (string == "") || !string {
keychainItem[(kSecAttrGeneric as! Any)] = self.getUUIDString()
}
}
class func readUUIDFromKeyChain() -> String {
var keychainItemm = KeychainItemWrapper(account: "Identfier", service: "AppName", accessGroup: nil)
var UUID = (keychainItemm[(kSecAttrGeneric as! Any)] as! String)
return UUID
}
class func getUUIDString() -> String {
var uuidRef = CFUUIDCreate(kCFAllocatorDefault)
var strRef = CFUUIDCreateString(kCFAllocatorDefault, uuidRef)
var uuidString = (strRef as! String).replacingOccurrencesOf("-", withString: "")
CFRelease(strRef)
CFRelease(uuidRef)
return uuidString
}
寫入UUID到keychain
我們最好在程序啟動之后把UUID寫入到keychain,代碼如下:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]) -> Bool {
AppUtils.saveUUIDToKeyChain()
}
讀取UUID
在需要讀取的地方直接調(diào)用AppUtils的類方法readUUIDFromKeyChain即可。
注意
1.設(shè)置非ARC編譯環(huán)境
因為KeychainItemWrapper.m文件是在非ARC環(huán)境下運行的,所以需要設(shè)置非arc編譯環(huán)境,
如圖所示:

2.讓同一開發(fā)商的所有APP在同一臺設(shè)備上獲取到UUID相同
在每個APP的項目里面做如下設(shè)置
2.1、設(shè)置accessgroup
var keychainItem = KeychainItemWrapper(account: "Identfier", service: "AppName", accessGroup: "YOUR_BUNDLE_SEED.com.yourcompany.userinfo")
此處設(shè)置accessGroup為YOUR_BUNDLE_SEED.com.yourcompany.userinfo
2.2、創(chuàng)建plist文件
然后在項目相同的目錄下創(chuàng)建KeychainAccessGroups.plist文件。
該文件的結(jié)構(gòu)是一個字典,其中中最頂層的節(jié)點必須是一個鍵為“keychain-access-groups”的Array,并且該Array中每一項都是一個描述分組的NSString。YOUR_BUNDLE_SEED.com.yourcompany.userinfo就是要設(shè)置的組名。
如圖:

2.3、 設(shè)置code signing
接著在Target--->Build Settings---->Code Signing欄下的Code Signing Entitlements右側(cè)添加KeychainAccessGroups.plist
如圖:
這樣就可以保證每個app都是從keychain中讀取出來同一個UUID