扩展字典,将字典存入钥匙链

这篇博客介绍了如何扩展字典功能,将其应用于钥匙链管理,详细阐述了在.h和.m文件中实现这一功能的方法。
摘要由CSDN通过智能技术生成

.h文件

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSDictionary (Keychain)

-(void) storeToKeychainWithKey:(NSString *)aKey;

+(NSDictionary *) dictionaryFromKeychainWithKey:(NSString *)aKey;

-(void) deleteFromKeychainWithKey:(NSString *)aKey;

@end

NS_ASSUME_NONNULL_END

.m文件

#import "NSDictionary+Keychain.h"

@implementation NSDictionary (Keychain)


-(void) storeToKeychainWithKey:(NSString *)aKey {
    // serialize dict
    NSString *error;
    NSData *serializedDictionary = [NSPropertyListSerialization dataFromPropertyList:self format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
    
    // encrypt in keychain
    if(!error) {
        // first, delete potential existing entries with this key (it won't auto update)
        [self deleteFromKeychainWithKey:aKey];
        
        // setup keychain storage properties
        NSDictionary *storageQuery = @{
                                       (id)kSecAttrAccount:    aKey,
                                       (id)kSecValueData:      serializedDictionary,
                                       (id)kSecClass:          (id)kSecClassGenericPassword,
                                       (id)kSecAttrAccessible: (id)kSecAttrAccessibleWhenUnlocked
                                       };
        OSStatus osStatus = SecItemAdd((CFDictionaryRef)storageQuery, nil);
        if(osStatus != noErr) {
            // do someting with error
//            NSLog(@"store error");
        }
    }
}


+(NSDictionary *) dictionaryFromKeychainWithKey:(NSString *)aKey {
    // setup keychain query properties
    NSDictionary *readQuery = @{
                                (id)kSecAttrAccount: aKey,
                                (id)kSecReturnData: (id)kCFBooleanTrue,
                                (id)kSecClass:      (id)kSecClassGenericPassword
                                };
    
    NSData *serializedDictionary = nil;
    OSStatus osStatus = SecItemCopyMatching((CFDictionaryRef)readQuery, (CFTypeRef *)&serializedDictionary);
    if(osStatus == noErr) {
        // deserialize dictionary
        NSString *error;
        NSDictionary *storedDictionary = [NSPropertyListSerialization propertyListFromData:serializedDictionary mutabilityOption:NSPropertyListImmutable format:nil errorDescription:&error];
        if(error) {
//            NSLog(@"%@", error);
        }
        [serializedDictionary release];
        return storedDictionary;
    }
    else {
        // do something with error
        return nil;
    }
}


-(void) deleteFromKeychainWithKey:(NSString *)aKey {
    // setup keychain query properties
    NSDictionary *deletableItemsQuery = @{
                                          (id)kSecAttrAccount:        aKey,
                                          (id)kSecClass:              (id)kSecClassGenericPassword,
                                          (id)kSecMatchLimit:         (id)kSecMatchLimitAll,
                                          (id)kSecReturnAttributes:   (id)kCFBooleanTrue
                                          };
    
    NSArray *itemList = nil;
    OSStatus osStatus;
    SecItemCopyMatching((CFDictionaryRef)deletableItemsQuery, (CFTypeRef *)&itemList);
    // each item in the array is a dictionary
    for (NSDictionary *item in itemList) {
        NSMutableDictionary *deleteQuery = [item mutableCopy];
        [deleteQuery setValue:(id)kSecClassGenericPassword forKey:(id)kSecClass];
        // do delete
        osStatus = SecItemDelete((CFDictionaryRef)deleteQuery);
        if(osStatus != noErr) {
            // do something with error
        }
        [deleteQuery release];
    }
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值