在开发App的时候,有一些数据是需要保存在App之外的,就是说即使用户删除了App,也要保留的这些数据。
例如(替代UDID,判断是否是第一次安装。。。。。。)
这样的数据就需要保存在系统的钥匙串中。 注意:保存在系统钥匙串中的数据也不是绝对不会被删除的,当用户重新刷机时会被清除掉。越狱后有一些工具可以修改这些数据。
下面提供实现:
LYKeyChain.h 文件:
#import <Foundation/Foundation.h>
@interface LYKeyChain : NSObject
// 保存数据
+ (void)save:(NSString *)key data:(id)data;
// 加载数据
+ (id)load:(NSString *)key;
// 删除数据
+ (void)delete:(NSString *)key;
@end
LYKeyChain.m 文件:
#import "LYKeyChain.h"
#import <Security/Security.h>
@implementation LYKeyChain
// 获取数据
+ (NSMutableDictionary *)keychainQuery:(NSString *)key
{
if (key) {
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
(__bridge_transfer id)kSecClassGenericPassword, (__bridge_transfer id)kSecClass,
key, (__bridge_transfer id)kSecAttrService,
key, (__bridge_transfer id)kSecAttrAccount,
(__bridge_transfer id)kSecAttrAccessibleAfterFirstUnlock, (__bridge_transfer id)kSecAttrAccessible,
nil];
}
return nil;
}
// 保存数据
+ (void)save:(NSString *)key data:(id)data
{
if (key && data != nil) {
//Get search dictionary
NSMutableDictionary *keychainQuery = [self keychainQuery:key];
//Delete old item before add new item
SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery);
//Add new object to search dictionary(Attention:the data format)
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge_transfer id)kSecValueData];
//Add item to keychain with the search dictionary
SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, NULL);
}
}
// 加载数据
+ (id)load:(NSString *)key
{
id ret = nil;
if (key) {
NSMutableDictionary *keychainQuery = [self keychainQuery:key];
//Configure the search setting
[keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge_transfer id)kSecReturnData];
[keychainQuery setObject:(__bridge_transfer id)kSecMatchLimitOne forKey:(__bridge_transfer id)kSecMatchLimit];
CFDataRef keyData = NULL;
if (SecItemCopyMatching((__bridge_retained CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
@try {
ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge_transfer NSData *)keyData];
}
@catch (NSException *e) {
NSLog(@"Unarchive of %@ failed: %@", key, e);
}
@finally {}
}
}
return ret;
}
// 删除数据
+ (void)delete:(NSString *)key
{
NSMutableDictionary *keychainQuery = [self keychainQuery:key];
SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery);
}
@end