UDID的全称是Unique Device Identifier,是苹果IOS设备的唯一识别码,由40个字符的字母和数字组成。在很多需要限制一台设备一个账号的应用中经常会用到。在iOS5中可以获取到设备的UDID,后来被苹果禁止了。

在 iOS 7 中,Apple 推荐使用广告标识符 advertisingIdentifier 来获取系统的唯一标识符。但是,用户如果重置了系统,广告标识符会重新生成。这就达不到 “唯一标识符” 的作用

IDFA (advertisingIdentifier)

获取唯一标识符办法

NSString *idfa =  [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString

有几种情况下,会重新生成广告标示符。

(1)如果用户完全重置系统((设置程序 -> 通用 -> 还原 -> 还原位置与隐私) ,这个广告标示符会重新生成。

(2)如果用户明确的还原广告(设置程序-> 通用 -> 关于本机 -> 广告 -> 还原广告标示符) ,那么广告标示符也会重新生成。

关于广告标示符需要注意

(1):如果程序在后台运行,此时用户“还原广告标示符”,然后再回到程序中,此时获取广 告标示符并不会立即获得还原后的标示符。必须要终止程序,然后再重新启动程序,才能获得还原后的广告标示符。

(2):由于idfa会出现取不到的情况,故绝不可以作为业务分析的主id,来识别用

IDFV(identifierForVendor)

获取唯一标识符办法


NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

每个设备在所属同一个Vender的应用里,都有相同的值。其中的Vender是指应用提供商,但准确点说,是通过BundleID的反转的前两部分进行匹配,如果相同就是同一个Vender,例如对于com.taobao.app1, com.taobao.app2 这两个BundleID来说,就属于同一个Vender,共享同一个idfv的值。

idfv一定可以获取到,但是如果设备上所有该供应商的应用被卸载,下载再安装时,会产生不同的idfv值

UUID

应用安装时,将UUID保存在keychain里,卸载下次安装,可从keychain中获取到。刷机、重装系统UUID会变化。



IDFA存储钥匙串

结构图如下


android 广告栏实现 安卓广告标识符_唯一标识




#import <AdSupport/AdSupport.h>
#import "SFHFKeychainUtils.h"

static NSString * const kDeviceIdentifier = @"kDeviceIdentifier";

- (NSString *)getDeviceIdentifier {
    //从钥匙串中获取唯一设备标识
    NSString * deviceIdentifier = [SFHFKeychainUtils getPasswordForUsername:kDeviceIdentifier andServiceName:[[NSBundle mainBundle] bundleIdentifier] error:nil];
    if (deviceIdentifier) {
        //如果钥匙串中存在唯一标识,则直接返回
        return deviceIdentifier;
    }
    //获取IDFA
    NSString *IDFA = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    //判断IDFA是否为空
    BOOL isEmpty = [[IDFA stringByReplacingOccurrencesOfString:@"-" withString:@""] stringByReplacingOccurrencesOfString:@"0" withString:@""].length;
    if (isEmpty) {
        //不为空,将IDFA作为唯一标识
        deviceIdentifier = IDFA;
    }
    else {
        //为空,获取UUID作为唯一标识
        deviceIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    }
    //保存唯一设备标识,如已存在则不进行任何处理
    [SFHFKeychainUtils storeUsername:kDeviceIdentifier andPassword:deviceIdentifier forServiceName:[[NSBundle mainBundle]bundleIdentifier] updateExisting:NO error:nil];
    //返回唯一标识
    return deviceIdentifier;
}