ylix

Point

Friday, 3 August 2012

Reading and Storing Values in Key Chain in iPhone Development

As we are ios developers we should know about Apple's Key Chain method , Apple provides this method to us for storing data securely in ios development, if you use NSDictionary method to store data you can not be accessed the data what you have been stored once your application deleted from the device and another important difference is ,the data what you stored by using NSDictionary can not be accessible from other applications, So to Overcome this problem,Apple provides an efficient method, to storing and retrieving from another app we can us Key Chain method,Here Lets see a simple way of accessing Key Chain,
1.Storing,
2.Deleting from Key Chain,
3. Retrieving from Key Chain,

for this i have created simple class which works fine for me...

in Header file(.h file)

#import <Foundation/Foundation.h>


@interface KeyChainAccess : NSObject

+ (void)saveData:(NSString *)KeyVal data:(id)data;
+ (id)getData:(NSString *)KeyVal ;
+ (void)deleteData:(NSString *)KeyVal ;

@end

in Implementation file(.m file)


#import "KeyChainAccess .h"

@implementation KeyChainAccess

+ (NSMutableDictionary *)getKeychainQuery:(NSString *)KeyVal {
    return [NSMutableDictionary dictionaryWithObjectsAndKeys:
            (id)kSecClassGenericPassword, (id)kSecClass,
            KeyVal , (id)kSecAttrService,
            KeyVal , (id)kSecAttrAccount,
            (id)kSecAttrAccessibleAfterFirstUnlock, (id)kSecAttrAccessible,
            nil];
}

+ (void)saveData:(NSString *)KeyVal data:(id)data {
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:KeyVal ];
    SecItemDelete((CFDictionaryRef)keychainQuery);
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
    SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
}

+ (id)getData:(NSString *)KeyVal {
    id ret = nil;
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:KeyVal ];
    [keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
    [keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
    CFDataRef keyData = NULL;
    if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
        @try {
            ret = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)keyData];
        }
        @catch (NSException *e) {
            NSLog(@"Unarchive of %@ failed: %@", service, e);
        }
        @finally {}
    }
    if (keyData) CFRelease(keyData);
    return ret;
}

+ (void)deleteData:(NSString *)KeyVal {
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:KeyVal ];
    SecItemDelete((CFDictionaryRef)keychainQuery);
}

@end


there are Four important methods in Key Chain Access

1. SecItemAdd:
        this method allows to add data to Key Chain.
2.SecItemDelete:
       this one allows to delete data from that.
3.SecItemCopyMatching:
       this allows us to read data from key chain and stored as CFDataRef
4. SecItemUpdate:
       this one useful allows us updating the data in key chain.


In getKeyChainQuery method i used  (id)kSecAttrAccessibleAfterFirstUnlock, (id)kSecAttrAccessible, this property is available only in iOS4.0 and later,,if you want use that method prior to iOS4.0 you can comment that property....


i hope that this one might be useful to you,Expecting your feedbacks..



No comments:

Share It

Infolinks In Text Ads