101 lines
3.1 KiB
Objective-C
Executable File
101 lines
3.1 KiB
Objective-C
Executable File
//
|
|
// PDKeyChain.m
|
|
// PDKeyChain
|
|
//
|
|
// Created by Panda on 16/8/23.
|
|
// Copyright © 2016年 v2panda. All rights reserved.
|
|
//
|
|
|
|
#import "PDKeyChain.h"
|
|
|
|
static NSString * const kPDKeyChainKey = @"com.shortplay.keychainKey";
|
|
|
|
@implementation PDKeyChain
|
|
|
|
+ (void)keyChainDelete{
|
|
[self delete:kPDKeyChainKey];
|
|
}
|
|
|
|
+ (NSDictionary *)getKeyChainData
|
|
{
|
|
NSDictionary *dic = [self load:kPDKeyChainKey];
|
|
if (!dic) {
|
|
dic = [NSDictionary dictionary];
|
|
}
|
|
return dic;
|
|
}
|
|
|
|
+ (void)setObject:(id)object forKey:(NSString *)key
|
|
{
|
|
NSMutableDictionary *tempDic = [[self getKeyChainData] mutableCopy];
|
|
[tempDic setObject:object forKey:key];
|
|
[self save:kPDKeyChainKey data:tempDic];
|
|
}
|
|
+ (id)objectForKey:(NSString *)key
|
|
{
|
|
NSDictionary *tempDic = [self getKeyChainData];
|
|
return tempDic[key];
|
|
}
|
|
+ (void)removeObjectForKey:(NSString *)key
|
|
{
|
|
NSMutableDictionary *tempDic = [[self getKeyChainData] mutableCopy];
|
|
[tempDic removeObjectForKey:key];
|
|
[self save:kPDKeyChainKey data:tempDic];
|
|
}
|
|
+ (void)removeAllObjects
|
|
{
|
|
NSMutableDictionary *tempDic = [[self getKeyChainData] mutableCopy];
|
|
[tempDic removeAllObjects];
|
|
[self save:kPDKeyChainKey data:tempDic];
|
|
}
|
|
|
|
|
|
|
|
+ (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 {
|
|
//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);
|
|
}
|
|
|
|
+ (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:(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(@"Unarchive of %@ failed: %@", service, e);
|
|
} @finally {
|
|
}
|
|
}
|
|
if (keyData)
|
|
CFRelease(keyData);
|
|
return ret;
|
|
}
|
|
|
|
+ (void)delete:(NSString *)service {
|
|
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
|
|
SecItemDelete((CFDictionaryRef)keychainQuery);
|
|
}
|
|
|
|
@end
|