HzUUIDTools.m 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // HzUUIDTools.m
  3. // powerone
  4. //
  5. // Created by clouder on 2021/7/23.
  6. // Copyright © 2021 onecloud.ltd. All rights reserved.
  7. //
  8. #import "HzUUIDTools.h"
  9. @implementation HzUUIDTools
  10. + (NSString *)getDeviceIDInKeychain
  11. {
  12. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];//获取app版本信息
  13. NSString *key = [NSString stringWithFormat:@"%@.uniqueid",[infoDictionary objectForKey:@"CFBundleIdentifier"]];
  14. NSString *getUDIDInKeychain = (NSString *)[HzUUIDTools load:key];
  15. MOLogV(@"从keychain中获取到的 UDID_INSTEAD %@",getUDIDInKeychain);
  16. if (!getUDIDInKeychain ||
  17. [getUDIDInKeychain isEqualToString:@""] ||
  18. [getUDIDInKeychain isKindOfClass:[NSNull class]])
  19. {
  20. getUDIDInKeychain = [UIDevice currentDevice].identifierForVendor.UUIDString;
  21. if(getUDIDInKeychain.length == 0 || [getUDIDInKeychain isEqualToString:@"00000000-0000-0000-0000-000000000000"])
  22. {
  23. CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
  24. CFStringRef uuidString = CFUUIDCreateString(kCFAllocatorDefault, uuidRef);
  25. getUDIDInKeychain = (NSString *)CFBridgingRelease(CFStringCreateCopy(NULL, uuidString));
  26. CFRelease(uuidRef);
  27. CFRelease(uuidString);
  28. MOLogV(@"\n \n \n _____重新存储 UUID _____\n \n \n %@",getUDIDInKeychain);
  29. }
  30. [HzUUIDTools save:key data:getUDIDInKeychain];
  31. }
  32. MOLogV(@"最终 ———— UDID_INSTEAD %@",getUDIDInKeychain);
  33. return getUDIDInKeychain;
  34. }
  35. + (NSMutableDictionary *)getKeychainQuery:(NSString *)service
  36. {
  37. return [NSMutableDictionary dictionaryWithObjectsAndKeys:
  38. (id)kSecClassGenericPassword,(id)kSecClass,
  39. service, (id)kSecAttrService,
  40. service, (id)kSecAttrAccount,
  41. (id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,
  42. nil];
  43. }
  44. + (void)save:(NSString *)service data:(id)data
  45. {
  46. //Get search dictionary
  47. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  48. //Delete old item before add new item
  49. SecItemDelete((CFDictionaryRef)keychainQuery);
  50. //Add new object to search dictionary(Attention:the data format)
  51. [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data]forKey:(id)kSecValueData];
  52. //Add item to keychain with the search dictionary
  53. SecItemAdd((CFDictionaryRef)keychainQuery,NULL);
  54. }
  55. + (id)load:(NSString *)service
  56. {
  57. id ret = nil;
  58. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  59. //Configure the search setting
  60. //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
  61. [keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
  62. [keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
  63. CFDataRef keyData = NULL;
  64. if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr)
  65. {
  66. @try
  67. {
  68. ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
  69. }
  70. @catch (NSException *e)
  71. {
  72. MOLogE(@"Unarchive of %@ failed: %@", service, e);
  73. }
  74. @finally
  75. {
  76. }
  77. }
  78. if (keyData)
  79. {
  80. CFRelease(keyData);
  81. }
  82. return ret;
  83. }
  84. + (void)delete:(NSString *)service
  85. {
  86. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  87. SecItemDelete((CFDictionaryRef)keychainQuery);
  88. }
  89. @end