JXKeyChainStore.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // JXKeyChainStore.m
  3. // shiku_im
  4. //
  5. // Created by IMAC on 2019/8/21.
  6. // Copyright © 2019 Reese. All rights reserved.
  7. //
  8. #import "JXKeyChainStore.h"
  9. #import <AdSupport/AdSupport.h>
  10. @implementation JXKeyChainStore
  11. + (NSMutableDictionary*)getKeychainQuery:(NSString*)service {
  12. return[NSMutableDictionary dictionaryWithObjectsAndKeys:
  13. (id)kSecClassGenericPassword,(id)kSecClass,
  14. service,(id)kSecAttrService,
  15. service,(id)kSecAttrAccount,
  16. (id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,
  17. nil];
  18. }
  19. + (void)save:(NSString*)service data:(id)data{
  20. NSMutableDictionary*keychainQuery = [self getKeychainQuery:service];
  21. SecItemDelete((CFDictionaryRef)keychainQuery);
  22. [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data]forKey:(id)kSecValueData];
  23. SecItemAdd((CFDictionaryRef)keychainQuery,NULL);
  24. }
  25. + (id)load:(NSString*)service {
  26. id ret =nil;
  27. NSMutableDictionary*keychainQuery = [self getKeychainQuery:service];
  28. [keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
  29. [keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
  30. CFDataRef keyData =NULL;
  31. if(SecItemCopyMatching((CFDictionaryRef)keychainQuery,(CFTypeRef*)&keyData) ==noErr){
  32. @try{
  33. ret =[NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData*)keyData];
  34. }@catch(NSException *e) {
  35. NSLog(@"Unarchiveof %@ failed: %@",service, e);
  36. }@finally{
  37. }
  38. }
  39. if(keyData)
  40. CFRelease(keyData);
  41. return ret;
  42. }
  43. + (void)deleteKeyData:(NSString*)service {
  44. NSMutableDictionary*keychainQuery = [self getKeychainQuery:service];
  45. SecItemDelete((CFDictionaryRef)keychainQuery);
  46. }
  47. + (NSString *)getUUIDByKeyChain{
  48. // 这个key的前缀最好是你的BundleID
  49. NSString*strUUID = (NSString*)[JXKeyChainStore load:@"com.shiku.im.push.usernamepassword"];
  50. //首次执行该方法时,uuid为空
  51. if([strUUID isEqualToString:@""]|| !strUUID)
  52. {
  53. // 获取UUID 这个是要引入<AdSupport/AdSupport.h>的
  54. strUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  55. if(strUUID.length ==0 || [strUUID isEqualToString:@"00000000-0000-0000-0000-000000000000"])
  56. {
  57. //生成一个uuid的方法
  58. CFUUIDRef uuidRef= CFUUIDCreate(kCFAllocatorDefault);
  59. strUUID = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault,uuidRef));
  60. CFRelease(uuidRef);
  61. CFUUIDRef puuid = CFUUIDCreate(nil);
  62. CFStringRef uuidString = CFUUIDCreateString(nil, puuid);
  63. NSString *result = (NSString *)CFBridgingRelease(CFStringCreateCopy(NULL, uuidString));
  64. NSMutableString *strUUID = result.mutableCopy;
  65. NSRange range = [strUUID rangeOfString:@"-"];
  66. while (range.location != NSNotFound) {
  67. [strUUID deleteCharactersInRange:range];
  68. range = [strUUID rangeOfString:@"-"];
  69. }
  70. NSLog(@"uuid%@",strUUID);
  71. }
  72. //将该uuid保存到keychain
  73. [JXKeyChainStore save:@"com.shiku.im.push.usernamepassword" data:strUUID];
  74. }
  75. return strUUID;
  76. }
  77. @end