123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- //
- // KeyChainTools.m
- // TeacherDaya
- //
- // Created by Kyle on 2020/12/24.
- // Copyright © 2020 DayaMusic. All rights reserved.
- //
- #import "KeyChainTools.h"
- #import <Security/Security.h>
- NSString *const KEY_ASSESSGROUP = @"B2AP53HHTU.Colexiu";
- @implementation KeyChainTools
- + (NSString *)getUUID {
- //定义存入keychain中的账号 也就是一个标识 表示是某个app存储的内容 bundle id就好
- NSString * const KEY_UUID = @"com.Colexiu.Kulexiu.UUID";
- // 测试用 清除keychain中的内容
- // [self delete:KEY_UUID];
- //读取UUID
- NSMutableDictionary *readUUID = (NSMutableDictionary *)[self load:KEY_UUID];
-
- //NSLog(@"keychain------><>%@",readUserPwd);
- if (!readUUID) {
- //如果为空 说明是第一次安装 做存储操作
- NSString *identifierStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
- identifierStr = [NSString stringToMD5:identifierStr];
- //NSLog(@"identifierStr-----><>%@",identifierStr);
- NSMutableDictionary *uuidDic = [NSMutableDictionary dictionaryWithObject:identifierStr forKey:KEY_UUID];
- [self save:KEY_UUID data:uuidDic];
- if (SUBMIT_UUID) {
- return identifierStr;
- }
- else {
- return nil;
- }
-
- }else{
- if (SUBMIT_UUID) {
- return [readUUID objectForKey:KEY_UUID];
- }
- else {
- return @"";
- }
- }
- }
-
- //储存
- + (void)save:(NSString *)service data:(id)data {
-
- //Get search dictionary
-
- NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
-
- //Delete old item before add new item
-
- SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
-
- //Add new object to search dictionary(Attention:the data format)
-
- [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData];
- //Add item to keychain with the search dictionary
-
- OSStatus status = SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL);
- NSLog(@"---%d", status);
- }
-
- + (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
-
- return [NSMutableDictionary dictionaryWithObjectsAndKeys:
-
- (__bridge id)kSecClassGenericPassword,(__bridge id)kSecClass,
-
- service, (__bridge id)kSecAttrService,
-
- service, (__bridge id)kSecAttrAccount,
-
- KEY_ASSESSGROUP, (__bridge id)kSecAttrAccessGroup,
-
- (__bridge id)kSecAttrAccessibleAfterFirstUnlock,(__bridge id)kSecAttrAccessible,
-
- nil];
- }
-
- //取出
- + (id)load:(NSString *)service {
-
- id ret = nil;
-
- NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
-
- //Configure the search setting
-
- //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
-
- [keychainQuery setObject:(__bridge id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
-
- [keychainQuery setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
- CFDataRef keyData = NULL;
-
- if (SecItemCopyMatching((__bridge CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
-
- @try {
-
- ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
-
- } @catch (NSException *e) {
-
- NSLog(@"Unarchive of %@ failed: %@", service, e);
-
- } @finally {
-
- }
-
- }
-
- if (keyData)
-
- CFRelease(keyData);
-
- return ret;
- }
- //删除
- + (void)delete:(NSString *)service {
- NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
- SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
- }
- @end
|