RcGameWQKeyChain.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // RcGameWQKeyChain.m
  3. // Vclusters_game_SDK_iOS
  4. //
  5. // Created by xd h on 2021/6/8.
  6. //
  7. #import "RcGameWQKeyChain.h"
  8. @implementation RcGameWQKeyChain
  9. static NSString * const KEY_IN_KEYCHAIN_RcGame = @"w10010DataPrivateX";
  10. static NSString * const KEY_Identifier_RcGame = @"wuuidIdentifierPrivateX";
  11. static NSString*identifierForVendor = nil;//设备标识符
  12. +(void)saveIdentifier:(NSString *)iden
  13. {
  14. NSMutableDictionary *usernamepasswordKVPairs = [NSMutableDictionary dictionary];
  15. [usernamepasswordKVPairs setObject:iden forKey:KEY_Identifier_RcGame];
  16. [RcGameWQKeyChain save:KEY_IN_KEYCHAIN_RcGame data:usernamepasswordKVPairs];
  17. }
  18. +(id)readIdentifier
  19. {
  20. NSMutableDictionary *userIdentifierKVPair = (NSMutableDictionary *)[RcGameWQKeyChain load:KEY_IN_KEYCHAIN_RcGame];
  21. return [userIdentifierKVPair objectForKey:KEY_Identifier_RcGame];
  22. }
  23. +(void)deleteIdentifier
  24. {
  25. [RcGameWQKeyChain delete:KEY_IN_KEYCHAIN_RcGame];
  26. }
  27. static NSMutableDictionary * queryDIC;
  28. + (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
  29. if (!queryDIC) {
  30. queryDIC =[NSMutableDictionary dictionaryWithObjectsAndKeys:
  31. (__bridge_transfer id)kSecClassGenericPassword,(__bridge_transfer id)kSecClass,
  32. service, (__bridge_transfer id)kSecAttrService,
  33. service, (__bridge_transfer id)kSecAttrAccount,
  34. (__bridge_transfer id)kSecAttrAccessibleAfterFirstUnlock,(__bridge_transfer id)kSecAttrAccessible,
  35. nil];
  36. }
  37. return queryDIC;
  38. }
  39. + (void)save:(NSString *)service data:(id)data {
  40. //Get search dictionary
  41. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  42. //Delete old item before add new item
  43. SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery);
  44. //Add new object to search dictionary(Attention:the data format)
  45. [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge_transfer id)kSecValueData];
  46. //Add item to keychain with the search dictionary
  47. SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, NULL);
  48. }
  49. + (id)load:(NSString *)service {
  50. id ret = nil;
  51. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  52. //Configure the search setting
  53. [keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge_transfer id)kSecReturnData];
  54. [keychainQuery setObject:(__bridge_transfer id)kSecMatchLimitOne forKey:(__bridge_transfer id)kSecMatchLimit];
  55. CFDataRef keyData = NULL;
  56. if (SecItemCopyMatching((__bridge_retained CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
  57. @try {
  58. ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge_transfer NSData *)keyData];
  59. } @catch (NSException *e) {
  60. NSLog(@"Unarchive of %@ failed: %@", service, e);
  61. } @finally {
  62. }
  63. }
  64. return ret;
  65. }
  66. + (void)delete:(NSString *)service {
  67. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  68. SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery);
  69. }
  70. /*******************************************************************************/
  71. + (NSString*)getOaidStringFun{
  72. if(!identifierForVendor)
  73. {//生成一个随机字符串
  74. NSString *randomUdidStr = [RcGameWQKeyChain readIdentifier];
  75. if(!randomUdidStr)//随机31个字符
  76. {
  77. randomUdidStr = [self randomString:31];
  78. [RcGameWQKeyChain saveIdentifier:randomUdidStr];
  79. }
  80. identifierForVendor = randomUdidStr;
  81. }
  82. return identifierForVendor;
  83. }
  84. /**
  85. 生成随机数字和大写字母
  86. @param number 需要的个数
  87. @return 生成的字符串
  88. */
  89. + (NSString *)randomString:(NSInteger)number {
  90. NSString *ramdom;
  91. NSMutableArray *array = [NSMutableArray array];
  92. for (int i = 1; i ; i ++) {
  93. int a = (arc4random() % 122);
  94. //48-57 (0-9)
  95. //65-90 (A-Z)
  96. if ((a >= 48 && a<=57) || (a >= 65 && a<=90)) {
  97. char c = (char)a;
  98. [array addObject:[NSString stringWithFormat:@"%c",c]];
  99. if (array.count == number) {
  100. break;
  101. }
  102. } else continue;
  103. }
  104. ramdom = [array componentsJoinedByString:@""];
  105. return ramdom;
  106. }
  107. @end