123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- //
- // JXKeyChainStore.m
- // shiku_im
- //
- // Created by IMAC on 2019/8/21.
- // Copyright © 2019 Reese. All rights reserved.
- //
- #import "JXKeyChainStore.h"
- #import <AdSupport/AdSupport.h>
- @implementation JXKeyChainStore
- + (NSMutableDictionary*)getKeychainQuery:(NSString*)service {
- return[NSMutableDictionary dictionaryWithObjectsAndKeys:
- (id)kSecClassGenericPassword,(id)kSecClass,
- service,(id)kSecAttrService,
- service,(id)kSecAttrAccount,
- (id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,
- nil];
- }
- + (void)save:(NSString*)service data:(id)data{
- NSMutableDictionary*keychainQuery = [self getKeychainQuery:service];
- SecItemDelete((CFDictionaryRef)keychainQuery);
- [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data]forKey:(id)kSecValueData];
- SecItemAdd((CFDictionaryRef)keychainQuery,NULL);
- }
- + (id)load:(NSString*)service {
- id ret =nil;
- NSMutableDictionary*keychainQuery = [self getKeychainQuery:service];
- [keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
- [keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
- CFDataRef keyData =NULL;
- if(SecItemCopyMatching((CFDictionaryRef)keychainQuery,(CFTypeRef*)&keyData) ==noErr){
- @try{
- ret =[NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData*)keyData];
- }@catch(NSException *e) {
- NSLog(@"Unarchiveof %@ failed: %@",service, e);
- }@finally{
- }
- }
- if(keyData)
- CFRelease(keyData);
- return ret;
- }
- + (void)deleteKeyData:(NSString*)service {
- NSMutableDictionary*keychainQuery = [self getKeychainQuery:service];
- SecItemDelete((CFDictionaryRef)keychainQuery);
- }
- + (NSString *)getUUIDByKeyChain{
- // 这个key的前缀最好是你的BundleID
- NSString*strUUID = (NSString*)[JXKeyChainStore load:@"com.shiku.im.push.usernamepassword"];
- //首次执行该方法时,uuid为空
- if([strUUID isEqualToString:@""]|| !strUUID)
- {
- // 获取UUID 这个是要引入<AdSupport/AdSupport.h>的
- strUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
-
- if(strUUID.length ==0 || [strUUID isEqualToString:@"00000000-0000-0000-0000-000000000000"])
- {
- //生成一个uuid的方法
- CFUUIDRef uuidRef= CFUUIDCreate(kCFAllocatorDefault);
- strUUID = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault,uuidRef));
- CFRelease(uuidRef);
- CFUUIDRef puuid = CFUUIDCreate(nil);
- CFStringRef uuidString = CFUUIDCreateString(nil, puuid);
- NSString *result = (NSString *)CFBridgingRelease(CFStringCreateCopy(NULL, uuidString));
- NSMutableString *strUUID = result.mutableCopy;
- NSRange range = [strUUID rangeOfString:@"-"];
- while (range.location != NSNotFound) {
- [strUUID deleteCharactersInRange:range];
- range = [strUUID rangeOfString:@"-"];
- }
- NSLog(@"uuid%@",strUUID);
- }
-
- //将该uuid保存到keychain
- [JXKeyChainStore save:@"com.shiku.im.push.usernamepassword" data:strUUID];
- }
- return strUUID;
- }
- @end
|