iOS 钥匙串存储用户数据

Posted 甘林梦的开发之路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 钥匙串存储用户数据相关的知识,希望对你有一定的参考价值。

参考: http://www.jianshu.com/p/f6d40065bb6c

 

#import <Foundation/Foundation.h>
#import <Security/Security.h>
@interface JCKeychainTool : NSObject

/**
 *  储存字符串到??钥匙串
 *
 *  @param sValue 对应的Value
 *  @param sKey   对应的Key
 */
+ (void)saveKeychainValue:(NSString *)sValue key:(NSString *)sKey;


/**
 *  从??钥匙串获取字符串
 *
 *  @param sKey 对应的Key
 *
 *  @return 返回储存的Value
 */
+ (NSString *)readKeychainValue:(NSString *)sKey;


/**
 *  从??钥匙串删除字符串
 *
 *  @param sKey 对应的Key
 */
+ (void)deleteKeychainValue:(NSString *)sKey;


@end

 

#import "JCKeychainTool.h"

@implementation JCKeychainTool

+ (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);
}
@end

 

 // 存
[JCKeychainTool saveKeychainValue:@"xxx" key:@"userName"];
[JCKeychainTool saveKeychainValue:@"yyy"key:@"password"];
// 读
[JCKeychainTool readKeychainValue:@"userName"];
[JCKeychainTool readKeychainValue:@"password"];
// 删
[JCKeychainTool deleteKeychainValue:@"userName"];
[JCKeychainTool deleteKeychainValue:@"password"];

以上是关于iOS 钥匙串存储用户数据的主要内容,如果未能解决你的问题,请参考以下文章

检索钥匙串中的数据以获取在 ios 中不起作用的特定服务

找不到用于存储“<用户名>”的钥匙串

iOS数据存储-钥匙串存储

存储 iOS 应用的用户密码和用户名

如何在ios钥匙串中手动存储?

iOS:新设备或恢复设备上的钥匙串