// // HzUUIDTools.m // powerone // // Created by clouder on 2021/7/23. // Copyright © 2021 onecloud.ltd. All rights reserved. // #import "HzUUIDTools.h" @implementation HzUUIDTools + (NSString *)getDeviceIDInKeychain { NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];//获取app版本信息 NSString *key = [NSString stringWithFormat:@"%@.uniqueid",[infoDictionary objectForKey:@"CFBundleIdentifier"]]; NSString *getUDIDInKeychain = (NSString *)[HzUUIDTools load:key]; MOLogV(@"从keychain中获取到的 UDID_INSTEAD %@",getUDIDInKeychain); if (!getUDIDInKeychain || [getUDIDInKeychain isEqualToString:@""] || [getUDIDInKeychain isKindOfClass:[NSNull class]]) { getUDIDInKeychain = [UIDevice currentDevice].identifierForVendor.UUIDString; if(getUDIDInKeychain.length == 0 || [getUDIDInKeychain isEqualToString:@"00000000-0000-0000-0000-000000000000"]) { CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault); CFStringRef uuidString = CFUUIDCreateString(kCFAllocatorDefault, uuidRef); getUDIDInKeychain = (NSString *)CFBridgingRelease(CFStringCreateCopy(NULL, uuidString)); CFRelease(uuidRef); CFRelease(uuidString); MOLogV(@"\n \n \n _____重新存储 UUID _____\n \n \n %@",getUDIDInKeychain); } [HzUUIDTools save:key data:getUDIDInKeychain]; } MOLogV(@"最终 ———— UDID_INSTEAD %@",getUDIDInKeychain); return getUDIDInKeychain; } + (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:(__bridge NSData *)keyData]; } @catch (NSException *e) { MOLogE(@"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