博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS获取UUID
阅读量:7095 次
发布时间:2019-06-28

本文共 3125 字,大约阅读时间需要 10 分钟。

转自:

参考:

总结一下:

1.代码采用CFUUID+KeyChain的实现方式。

2.CFUUID、IDFA、IDFV都是可变的(其它方法,比如苹果自带的UUID和MAC地址基本上被禁止使用),但是IDFA和IDFV不一定都能获取到(iOS系统不同版本不一定支持),所以直接选择了CFUUID(iOS2.0就开始支持了)。

 

PS:

3.代码中使用stringByReplacingOccurrencesOfString将间隔符-去掉了(原来是这种:CAE1CC3A-92C1-45A5-9F55-932264020358,去掉后变为CAE1CC3A92C145A59F55932264020358)。

4.SdkMgr为自定义类名,根据需要自行修改。

NSString * const KEY_UDID_INSTEAD = @"com.app.uuid.test";+(NSString *)getDeviceUUID{    NSString *getUDIDInKeychain = (NSString *)[SdkMgr load:KEY_UDID_INSTEAD];    if (!getUDIDInKeychain ||[getUDIDInKeychain isEqualToString:@""]||[getUDIDInKeychain isKindOfClass:[NSNull class]]) {        CFUUIDRef puuid = CFUUIDCreate( nil );        CFStringRef uuidString = CFUUIDCreateString( nil, puuid );        NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));        CFRelease(puuid);        CFRelease(uuidString);                //去掉横线,保留32位长度        NSString *rst = [result stringByReplacingOccurrencesOfString:@"-" withString:@""];        [SdkMgr save:KEY_UDID_INSTEAD data:rst];        getUDIDInKeychain = (NSString *)[SdkMgr load:KEY_UDID_INSTEAD];    }    return getUDIDInKeychain;}#pragma mark - private+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {    return [NSMutableDictionary dictionaryWithObjectsAndKeys:            (id)kSecClassGenericPassword,(id)kSecClass,            service, (id)kSecAttrService,            service, (id)kSecAttrAccount,            (id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,            nil];}+ (void)save:(NSString *)service data:(id)data {    //Get search dictionary    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];    //Delete old item before add new item    SecItemDelete((CFDictionaryRef)keychainQuery);    //Add new object to search dictionary(Attention:the data format)    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];    //Add item to keychain with the search dictionary    SecItemAdd((CFDictionaryRef)keychainQuery, NULL);}+ (id)load:(NSString *)service {    id ret = nil;    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];    //Configure the search setting    //Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue    [keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];    [keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];    CFDataRef keyData = NULL;    if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {        @try {            ret = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)keyData];        } @catch (NSException *e) {            NSLog(@"Unarchive of %@ failed: %@", service, e);        } @finally {        }    }    if (keyData)    CFRelease(keyData);    return ret;}+ (void)delete:(NSString *)service {    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];    SecItemDelete((CFDictionaryRef)keychainQuery);}@end

转载于:https://www.cnblogs.com/chevin/p/8213877.html

你可能感兴趣的文章
php pdo错误:SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
查看>>
Team Foundation Server:CodeUI Automation Test 学习笔记4
查看>>
Linux 小知识翻译 - 「cron」
查看>>
docker 一些简略环境搭建及部分链接
查看>>
android studio获取SHA1
查看>>
怎么才能在windows使用git命令
查看>>
Sigar应用
查看>>
从单体架构到微服务的演变之路
查看>>
Valgrind内存泄露检测工具使用初步
查看>>
PDF 补丁丁 0.5.0.2657 发布
查看>>
vue之axios使用
查看>>
VBA批量删除excel表高级版
查看>>
docker & nodejs & mongodb
查看>>
css 清除浮动
查看>>
Python_Selenium学习笔记(2)-浏览器操作方法
查看>>
excel自定义函数添加和使用方法
查看>>
C# 压缩组件介绍与入门
查看>>
结对学习心得感想及创意照
查看>>
sug
查看>>
windows 环境变量
查看>>