1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- //
- // YostarKeychain.m
- // YostarUtilits
- //
- // Created by Yostar on 2018/6/21.
- // Copyright © 2018年 Yostar. All rights reserved.
- //
- #import "YostarKeychain.h"
- #import <Security/Security.h>
- @implementation YostarKeychain
- + (NSMutableDictionary *)getKeychainQuery:(NSString *)service{
- return [NSMutableDictionary dictionaryWithObjectsAndKeys:
- (id)kSecClassGenericPassword, (id)kSecClass,
- service, (id)kSecAttrService,
- service, (id)kSecAttrAccount,
- (id)kSecAttrAccessibleAfterFirstUnlock, (id)kSecAttrAccessible, nil];
- }
- #pragma mark - 写入
- + (void)save:(NSString *)service data:(id)data{
- //Get search dictionary
- NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
- //Delete old item before add new item
- SecItemDelete((CFDictionaryRef)keychainQuery);
- //Add new object to search dictionary(Attention:the data format)
- [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
- //Add item to keychain with the search dictionary
- SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
- }
- #pragma mark - 读取
- + (id)load:(NSString *)service{
- id result = 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:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
- [keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
- CFDataRef keyData = NULL;
- if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
- @try {
- result = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
- // result = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge_transfer NSData *)keyData];
- } @catch (NSException *exception) {
- NSLog(@"Unarchive of %@ failed: %@", service, exception);
- } @finally {
-
- }
- }
- if (keyData) {
- CFRelease(keyData);
- }
- return result;
- }
- #pragma mark - 删除
- + (void)delete:(NSString *)service{
- NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
- SecItemDelete((CFDictionaryRef)keychainQuery);
- }
- @end
|