Keychain Add Update Remove and Find operations using one file.

suggest change
#import <Foundation/Foundation.h>
typedef void (^KeychainOperationBlock)(BOOL successfulOperation, NSData *data, OSStatus status);

@interface Keychain : NSObject

-(id) initWithService:(NSString *) service_ withGroup:(NSString*)group_;

-(void)insertKey:(NSString *)key withData:(NSData *)data withCompletion:(KeychainOperationBlock)completionBlock;
-(void)updateKey:(NSString*)key withData:(NSData*) data withCompletion:(KeychainOperationBlock)completionBlock;
-(void)removeDataForKey:(NSString*)key withCompletionBlock:(KeychainOperationBlock)completionBlock;
-(void)findDataForKey:(NSString*)key withCompletionBlock:(KeychainOperationBlock)completionBlock;

@end

Keychain.m

#import "Keychain.h"
#import <Security/Security.h>

@implementation Keychain

{
   NSString * keychainService;
   NSString * keychainGroup;
}

-(id) initWithService:(NSString *)service withGroup:(NSString*)group
{
   self =[super init];
   if(self) {
       keychainService = [NSString stringWithString:service];
       if(group) {
           keychainGroup = [NSString stringWithString:group];
       }
   }
   
   return  self;
}

-(void)insertKey:(NSString *)key
       withData:(NSData *)data
 withCompletion:(KeychainOperationBlock)completionBlock
{
   NSMutableDictionary * dict =[self prepareDict:key];
   [dict setObject:data forKey:(__bridge id)kSecValueData];
   [dict setObject:keychainService forKey:(id)kSecAttrService];
   
   OSStatus status = SecItemAdd((__bridge CFDictionaryRef)dict, NULL);
   if (status == errSecDuplicateItem) {
       [self updateKey:key withData:data withCompletion:completionBlock];
       return;
   }
   if (status != errSecSuccess) {
       NSLog(@"Unable to add item with key =%@ error:%d", key, (int)status);
   }
   if (completionBlock) {
       completionBlock(status == errSecSuccess, nil, status);
   }
}

-(void)findDataForKey:(NSString *)key
 withCompletionBlock:(KeychainOperationBlock)completionBlock
{
   NSMutableDictionary *dict = [self prepareDict:key];
   [dict setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
   [dict setObject:keychainService forKey:(id)kSecAttrService];
   [dict setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
   CFTypeRef result = NULL;
   OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)dict,&result);
   
   if( status != errSecSuccess) {
       NSLog(@"Unable to fetch item for key %@ with error:%d", key, (int)status);
       if (completionBlock) {
           completionBlock(NO, nil, status);
       }
   } else {
       NSData *data = (__bridge_transfer NSData *)result;
       if (completionBlock) {
           completionBlock(YES, data, status);
       }
   }
}

-(void)updateKey:(NSString *)key
       withData:(NSData *)data
 withCompletion:(KeychainOperationBlock)completionBlock
{
   NSMutableDictionary * dictKey =[self prepareDict:key];
   
   NSMutableDictionary * dictUpdate =[[NSMutableDictionary alloc] init];
   [dictUpdate setObject:data forKey:(__bridge id)kSecValueData];
   [dictUpdate setObject:keychainService forKey:(id)kSecAttrService];
   OSStatus status = SecItemUpdate((__bridge CFDictionaryRef)dictKey, (__bridge CFDictionaryRef)dictUpdate);
   if( status != errSecSuccess) {
       NSLog(@"Unable to update item for key %@ with error:%d", key, (int)status);
   }
   if (completionBlock) {
       completionBlock(errSecSuccess == status, nil, status);
   }
}

-(void)removeDataForKey:(NSString *)key
   withCompletionBlock:(KeychainOperationBlock)completionBlock {
   NSMutableDictionary *dict = [self prepareDict:key];
   OSStatus status = SecItemDelete((__bridge CFDictionaryRef)dict);
   if( status != errSecSuccess) {
       NSLog(@"Unable to remove item for key %@ with error:%d", key, (int)status);
   }
   if (completionBlock) {
       completionBlock(errSecSuccess == status, nil, status);
   }
}

#pragma mark Internal methods

-(NSMutableDictionary*) prepareDict:(NSString *) key {
   
   NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
   [dict setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
   
   NSData *encodedKey = [key dataUsingEncoding:NSUTF8StringEncoding];
   [dict setObject:encodedKey forKey:(__bridge id)kSecAttrGeneric];
   [dict setObject:encodedKey forKey:(__bridge id)kSecAttrAccount];
   [dict setObject:keychainService forKey:(__bridge id)kSecAttrService];
   [dict setObject:(__bridge id)kSecAttrAccessibleAlwaysThisDeviceOnly forKey:(__bridge id)kSecAttrAccessible];
   
   //This is for sharing data across apps
   if(keychainGroup != nil) {
       [dict setObject:keychainGroup forKey:(__bridge id)kSecAttrAccessGroup];
   }
   
   return  dict;
}

@end

DLog is not a standard function, so the logs above use NSLog. On a duplicate item, insertKey:withData:withCompletion: updates once and calls the completion only from that update. A successful insert calls the completion once. __bridge_transfer gives the found NSData to ARC; a plain __bridge cast would leak it. kSecAttrAccessibleAlwaysThisDeviceOnly is deprecated, and items stored with it can be read while the device is locked. The sample keeps that protection class. If group is nil, the group ivar stays nil under ARC.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents