JXUUIDModel.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // JXUUIDModel.m
  3. // shiku_im
  4. //
  5. // Created by os on 2020/6/24.
  6. // Copyright © 2020 Reese. All rights reserved.
  7. //
  8. #import "JXUUIDModel.h"
  9. #import <Security/Security.h>
  10. #import "KeychainUUID.h"
  11. NSString * const KEY_UDID_INSTEAD = @"app.jixincc.vip";
  12. @implementation JXUUIDModel
  13. -(NSString *)keyUDid{
  14. KeychainUUID *keychain = [[KeychainUUID alloc] init];
  15. id data = [keychain readUDID];
  16. return data;
  17. }
  18. +(NSString *)getDeviceIDInKeychain
  19. {
  20. NSString *getUDIDInKeychain = (NSString *)[JXUUIDModel load:KEY_UDID_INSTEAD];
  21. // NSLog(@"从keychain中获取到的 UDID_INSTEAD %@",getUDIDInKeychain);
  22. if (!getUDIDInKeychain ||[getUDIDInKeychain isEqualToString:@""]||[getUDIDInKeychain isKindOfClass:[NSNull class]]) {
  23. CFUUIDRef puuid = CFUUIDCreate( nil );
  24. CFStringRef uuidString = CFUUIDCreateString( nil, puuid );
  25. NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
  26. CFRelease(puuid);
  27. CFRelease(uuidString);
  28. NSLog(@"\n \n \n _____重新存储 UUID _____\n \n \n %@",result);
  29. [JXUUIDModel save:KEY_UDID_INSTEAD data:result];
  30. getUDIDInKeychain = (NSString *)[JXUUIDModel load:KEY_UDID_INSTEAD];
  31. }
  32. // NSLog(@"最终 ———— UDID_INSTEAD %@",getUDIDInKeychain);
  33. return getUDIDInKeychain;
  34. }
  35. #pragma mark - private
  36. + (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
  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. //Get search dictionary
  46. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  47. //Delete old item before add new item
  48. SecItemDelete((CFDictionaryRef)keychainQuery);
  49. //Add new object to search dictionary(Attention:the data format)
  50. [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
  51. //Add item to keychain with the search dictionary
  52. SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
  53. }
  54. + (id)load:(NSString *)service {
  55. id ret = nil;
  56. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  57. //Configure the search setting
  58. //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
  59. [keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
  60. [keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
  61. CFDataRef keyData = NULL;
  62. if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
  63. @try {
  64. ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
  65. } @catch (NSException *e) {
  66. NSLog(@"Unarchive of %@ failed: %@", service, e);
  67. } @finally {
  68. }
  69. }
  70. if (keyData)
  71. CFRelease(keyData);
  72. return ret;
  73. }
  74. + (void)delete:(NSString *)service {
  75. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  76. SecItemDelete((CFDictionaryRef)keychainQuery);
  77. }
  78. @end