// // RcGameWQKeyChain.m // Vclusters_game_SDK_iOS // // Created by xd h on 2021/6/8. // #import "RcGameWQKeyChain.h" @implementation RcGameWQKeyChain static NSString * const KEY_IN_KEYCHAIN_RcGame = @"w10010DataPrivateX"; static NSString * const KEY_Identifier_RcGame = @"wuuidIdentifierPrivateX"; static NSString*identifierForVendor = nil;//设备标识符 +(void)saveIdentifier:(NSString *)iden { NSMutableDictionary *usernamepasswordKVPairs = [NSMutableDictionary dictionary]; [usernamepasswordKVPairs setObject:iden forKey:KEY_Identifier_RcGame]; [RcGameWQKeyChain save:KEY_IN_KEYCHAIN_RcGame data:usernamepasswordKVPairs]; } +(id)readIdentifier { NSMutableDictionary *userIdentifierKVPair = (NSMutableDictionary *)[RcGameWQKeyChain load:KEY_IN_KEYCHAIN_RcGame]; return [userIdentifierKVPair objectForKey:KEY_Identifier_RcGame]; } +(void)deleteIdentifier { [RcGameWQKeyChain delete:KEY_IN_KEYCHAIN_RcGame]; } static NSMutableDictionary * queryDIC; + (NSMutableDictionary *)getKeychainQuery:(NSString *)service { if (!queryDIC) { queryDIC =[NSMutableDictionary dictionaryWithObjectsAndKeys: (__bridge_transfer id)kSecClassGenericPassword,(__bridge_transfer id)kSecClass, service, (__bridge_transfer id)kSecAttrService, service, (__bridge_transfer id)kSecAttrAccount, (__bridge_transfer id)kSecAttrAccessibleAfterFirstUnlock,(__bridge_transfer id)kSecAttrAccessible, nil]; } return queryDIC; } + (void)save:(NSString *)service data:(id)data { //Get search dictionary NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; //Delete old item before add new item SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery); //Add new object to search dictionary(Attention:the data format) [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge_transfer id)kSecValueData]; //Add item to keychain with the search dictionary SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, NULL); } + (id)load:(NSString *)service { id ret = nil; NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; //Configure the search setting [keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge_transfer id)kSecReturnData]; [keychainQuery setObject:(__bridge_transfer id)kSecMatchLimitOne forKey:(__bridge_transfer id)kSecMatchLimit]; CFDataRef keyData = NULL; if (SecItemCopyMatching((__bridge_retained CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) { @try { ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge_transfer NSData *)keyData]; } @catch (NSException *e) { NSLog(@"Unarchive of %@ failed: %@", service, e); } @finally { } } return ret; } + (void)delete:(NSString *)service { NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery); } /*******************************************************************************/ + (NSString*)getOaidStringFun{ if(!identifierForVendor) {//生成一个随机字符串 NSString *randomUdidStr = [RcGameWQKeyChain readIdentifier]; if(!randomUdidStr)//随机31个字符 { randomUdidStr = [self randomString:31]; [RcGameWQKeyChain saveIdentifier:randomUdidStr]; } identifierForVendor = randomUdidStr; } return identifierForVendor; } /** 生成随机数字和大写字母 @param number 需要的个数 @return 生成的字符串 */ + (NSString *)randomString:(NSInteger)number { NSString *ramdom; NSMutableArray *array = [NSMutableArray array]; for (int i = 1; i ; i ++) { int a = (arc4random() % 122); //48-57 (0-9) //65-90 (A-Z) if ((a >= 48 && a<=57) || (a >= 65 && a<=90)) { char c = (char)a; [array addObject:[NSString stringWithFormat:@"%c",c]]; if (array.count == number) { break; } } else continue; } ramdom = [array componentsJoinedByString:@""]; return ramdom; } @end