iOS 获取设备唯一标示符

iOS一直都比较注重用户隐私,在 iOS 5.0之后系统UDID方法就被废弃掉了,iOS 7中mac地址又被封杀,所有获取mac都将返回统一值02:00:00..,虽然iOS 6.0新增了两个用于替换UDID的接口,分别是:identifierForVendor,advertisingIdentifier(有时可能获取不到),但这两个接口会在相同开发组下的应用删除完后重新安装应用时数值会发生改变,并不是唯一的标示符。经过对钥匙串及iCloud同步的多方面了解,如果通过系统钥匙串保存一串字符串,且不允许它同步到其他设备,这样不就可以生成一个真正意义上的唯一ID了麽,而且还不会被苹果封杀,即使identifierForVendor的值发生了改变它也依然不变,除非执行设备“抹除所有内容和设置”这一项操作。鉴于网上很多类似原理生成唯一ID的资料要么是依赖了SAMKeyChains库,要么是需要加载KeychainItemWrapper类文件,感觉有点麻烦,于是动手封装了以下类,它不需要你加载其它类库,随拷贝随用。废话不多说,直接上代码,有问题欢迎指正,不喜勿喷,和谐共享。

Github源码下载地址


首先创建UIDevice分类

#import 

@interface UIDevice (DeviceIDByKeychainThisDeviceOnly)

+ (NSString*)identifierByKeychain;

@end

#import 

@implementation UIDevice (DeviceIDByKeychainThisDeviceOnly)

+ (NSString*)identifierByKeychain;
{
    //该类方法没有线程保护,所以可能因异步而导致创建出不同的设备唯一ID,故而增加此线程锁!
    @synchronized ([NSNotificationCenter defaultCenter])
    {
        NSString* service = @"CreateDeviceIdentifierByKeychain";
        NSString* account = @"VirtualDeviceIdentifier";
        //获取iOS系统推荐的设备唯一ID
        NSString* recommendDeviceIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        recommendDeviceIdentifier = [recommendDeviceIdentifier stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSMutableDictionary* queryDic = [NSMutableDictionary dictionary];
        [queryDic setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
        [queryDic setObject:service forKey:(__bridge id)kSecAttrService];
        [queryDic setObject:(__bridge id)kCFBooleanFalse forKey:(__bridge id)kSecAttrSynchronizable];
        [queryDic setObject:account forKey:(__bridge id)kSecAttrAccount];
        [queryDic setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];//默认值为kSecMatchLimitOne,表示返回结果集的第一个
        [queryDic setObject:(__bridge id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
        CFTypeRef keychainPassword = NULL;
        //首先查询钥匙串是否存在对应的值,如果存在则直接返回钥匙串中的值
        OSStatus queryResult = SecItemCopyMatching((__bridge CFDictionaryRef)queryDic, &keychainPassword);
        if (queryResult == errSecSuccess)
        {
            NSString *pwd = [[NSString alloc] initWithData:(__bridge NSData * _Nonnull)(keychainPassword) encoding:NSUTF8StringEncoding];
            if ([pwd isKindOfClass:[NSString class]] && pwd.length > 0)
            {
                return pwd;
            }
            else
            {
                //如果钥匙串中的相关数据不合法,则删除对应的数据重新创建
                NSMutableDictionary* deleteDic = [NSMutableDictionary dictionary];
                [deleteDic setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
                [deleteDic setObject:service forKey:(__bridge id)kSecAttrService];
                [deleteDic setObject:(__bridge id)kCFBooleanFalse forKey:(__bridge id)kSecAttrSynchronizable];
                [deleteDic setObject:account forKey:(__bridge id)kSecAttrAccount];
                OSStatus status = SecItemDelete((__bridge CFDictionaryRef)deleteDic);
                if (status != errSecSuccess)
                {
                    return recommendDeviceIdentifier;
                }
            }
        }
        if (recommendDeviceIdentifier.length > 0)
        {
            //创建数据到钥匙串,达到APP即使被删除也不会变更的设备唯一ID,除非系统抹除数据,否则该数据将存储在钥匙串中
            NSMutableDictionary* createDic = [NSMutableDictionary dictionary];
            [createDic setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
            [createDic setObject:service forKey:(__bridge id)kSecAttrService];
            [createDic setObject:account forKey:(__bridge id)kSecAttrAccount];
            [createDic setObject:(__bridge id)kCFBooleanFalse forKey:(__bridge id)kSecAttrSynchronizable];//不可以使用iCloud同步钥匙串数据,否则导致同一个iCloud账户的多个设备获取的唯一ID相同
            [createDic setObject:(__bridge id)kSecAttrAccessibleAlwaysThisDeviceOnly forKey:(__bridge id)kSecAttrAccessible];//增加一道保险,防止钥匙串数据被同步到其他设备,保证设备ID绝对唯一。
            [createDic setObject:[recommendDeviceIdentifier dataUsingEncoding:NSUTF8StringEncoding] forKey:(__bridge id)kSecValueData];
            OSStatus createResult = SecItemAdd((__bridge CFDictionaryRef)createDic, nil);
            if (createResult != errSecSuccess)
            {
                NSLog(@"通过钥匙串创建设备唯一ID不成功!");
            }
        }
        return recommendDeviceIdentifier;
    }
}

@end

使用示例,首先关联分类头文件

NSString* test = [UIDevice identifierByKeychain];
NSLog(@"%@", test);

你可能感兴趣的:(iOS 获取设备唯一标示符)