iOS系统存储的数据都是在sandBox里面, 一旦删除App,沙盒也就不存在了,要想在手机中一直保存用户的个人信息 ,即使删除App之后,再次安装还有此信息,那么就可以用keychain(钥匙串)来保存信息。
通常情况下,iOS系统用NSUserDefaults存储数据信息, 但是对于一些私密信息, 比如密码,证书等等,就需要使用更为安全的keychain了,keychain里保存的信息不会因为App被删除而丢失,所以,可以利用这个keychain这个特点来保存设备唯一标识。新浪微博和百度都使用了该方式。
那么,如何在应用里使用keyChain呢, 我们需要导入Security.framework框架, keyChain的操作接口声明在头文件SecItem.h里.直接使用SecItem.h里面方法操作keychain.
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service{
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
(__bridge_transfer id)kSecClassGenericPassword,
(__bridge_transfer id)kSecClass,service,
(__bridge_transfer id)kSecAttrService,service,
(__bridge_transfer id)kSecAttrAccount,
(__bridge_transfer id)kSecAttrAccessibleAfterFirstUnlock,
(__bridge_transfer id)kSecAttrAccessible,
nil];
}
+ (void)saveKeychainValue:(NSString *)sValue key:(NSString *)sKey{
NSMutableDictionary * keychainQuery = [self getKeychainQuery:sKey];
SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery);
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:sValue] forKey:(__bridge_transfer id)kSecValueData];
SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, NULL);
}
+ (NSString *)readKeychainValue:(NSString *)sKey
{
NSString *ret = nil;
NSMutableDictionary *keychainQuery = [self getKeychainQuery:sKey];
[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 CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
@try {
ret = (NSString *)[NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
} @catch (NSException *e) {
NSLog(@"Unarchive of %@ failed: %@", sKey, e);
} @finally {
}
}
if (keyData)
CFRelease(keyData);
return ret;
}
+ (void)deleteKeychainValue:(NSString *)sKey {
NSMutableDictionary *keychainQuery = [self getKeychainQuery:sKey];
SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
}