1. 程式人生 > >[iOS]iOS8可用的識別使用者方式(IDFA、UUID、IDFV)

[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就可滿足,也有一些公司會將多種方式結合起來使用,具體看公司需求。