[iOS]iOS8可用的識別用戶方式(IDFA、UUID、IDFV)

本文地址:http://blog.csdn.net/zhaoyabei/article/details/46682765 轉載註明出處

想要追蹤、統計用戶,自然離不開用戶唯一標識符,這是每個公司都面臨的問題。在歷史上唯一標識符很多,如UDID、MAC地址、OpenUDID等,不再一一介紹他們是怎麼掛掉的,現在好用的只剩下了idfa、idfv、UUID+keyChain。


IDFA(Advertising Identifier)

可以理解爲廣告id,Apple公司提供的用於追蹤用戶的廣告標識符。

  • 缺點:用戶可通過設置-隱私-廣告-還原廣告標識符 還原,之後會得新的到標識符;
  • 要求iOS>=6.0。
  • 使用:
#import <AdSupport/AdSupport.h>
NSString *idfa= [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  • 1
  • 2

IDFV (IdentifierForVendor)

Apple提供給Vendor的唯一標識符,Vendor代表了應用開發商.

實際使用時,一個Vendor具體定義如下:

  • iOS6:Bundle ID(CFBundleIdentifier)的前兩位,如果Bundle ID 只有一位,那就使用全部Bundle ID。

  • iOS7: Bundle ID(CFBundleIdentifier除去最後一部分的內容. 如果Bundle ID 只有一位,那就使用全部Bundle ID。

例如,com.baidu.tieba 和 com.baidu.image 所有情況下得到的idfv是相同的,因爲它們的CFBundleIdentifier 前兩部分是相同的。com.baidu.tieba.a 和 com.baidu.image.b在iOS6得到的idfv相同,在iOS7及以上得到的idfv就不同。


Apple官方文檔上的說明:

The value of this property is the same for apps that come from the same vendor running on the same device. A different value is returned for apps on the same device that come from different vendors, and for apps on different devices regardless of vendor.

Normally, the vendor is determined by data provided by the App Store. If the app was not installed from the app store (such as enterprise apps and apps still in development), then a vendor identifier is calculated based on the app’s bundle ID. The bundle ID is assumed to be in reverse-DNS format.

  • On iOS 6, the first two components of the bundle ID are used to generate the vendor ID. if the bundle ID only has a single component, then the entire bundle ID is used.

  • On IOS 7, all components of the bundle except for the last component are used to generate the vendor ID. If the bundle ID only has a single component, then the entire bundle ID is used.

  • 缺點:把同一個開發商的所有應用卸載後,再次安裝取到的idfv會不同。假設手機上裝有公司的兩款app:XX貼吧、XX微博,兩個APP同時被卸載後,再次安裝獲得的IDFV就跟原來不同了。
  • 要求:iOS>=6.0
  • 使用:
NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
  • 1
  • 2

UUID(Universally Unique Identifier)

通用唯一識別碼,每次生成均不一樣,所以第一次生成後需要保存到鑰匙串,這樣即使應用刪除再重裝仍然可以從鑰匙串得到它。

  • 使用: 
    UUID生成方法很多種,這裏只寫出一種。生成一個UUID:
-(NSString*) uuid {
    CFUUIDRef puuid = CFUUIDCreate( nil );
    CFStringRef uuidString = CFUUIDCreateString( nil, puuid );
    NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
    CFRelease(puuid);
    CFRelease(uuidString);
    return result;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

將UUID儲存在鑰匙串,這裏用到了一個第三方的工具:SFHFKeychainUtils:GitHub地址

 [SFHFKeychainUtils storeUsername:@"UDID" andPassword:[self uuid] forServiceName:@"ZYB" updateExisting:1 error:nil];
  • 1

從鑰匙串取出UUID:

[SFHFKeychainUtils getPasswordForUsername:@"UDID" andServiceName:@"ZYB" error:nil];
  • 1

注意,如果沒有存儲就直接取出會crash。


一般情況下使用第三種UUID就可滿足,也有一些公司會將多種方式結合起來使用,具體看公司需求。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章