[iOS]iOS8可用的识别用户方式(IDFA、UUID、IDFV)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zyb050607/article/details/46682765

本文地址: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就可满足,也有一些公司会将多种方式结合起来使用,具体看公司需求。

猜你喜欢

转载自blog.csdn.net/zyb050607/article/details/46682765
今日推荐