Skip to main content

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,
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 ;


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,

+ (void)saveData:(NSString *)KeyVal data:(id)data {
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:KeyVal ];
    [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 ];


there are Four important methods in Key Chain Access

1. SecItemAdd:
        this method allows to add data to Key Chain.
       this one allows to delete data from that.
       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..


Popular posts from this blog

ERROR: Unable to load VM from snapshot. The snapshot has been saved for a different hardware configuration.

the problem is not in your project do these steps,it will be solved

Edit the AVD to disable the Snapshot. orWhen you next start the AVD, uncheck the Launch with snapshot, but keep Save snapshot checked. This way the snapshot will be ignored at start-up, but overwritten when you exit. You will then be able to use snapshot for subsequent runs as before.hope that it might be useful to you

How to create UIWebView Programatically in iPhone development

Here we are going to create UIWebView programatically,here is code for your reference , as we know webview use to load html strings and url load, Apple provides UIWebView to achieve this task,

//initialize  and create the UIWebView
UIWebView  *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
aWebView.autoresizesSubviews = YES;
                    aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight |                 UIViewAutoresizingFlexibleWidth);

                   //set the web view delegates for the web view to be itself
                   aWebView.delegate = self;

                  //Set the URL to go to for your UIWebView
                  NSString *urlAddress = @””;

                  //Create a URL object.
                   NSURL *url = [NSURL URLWithString:urlAddress];

                  //URL Requst Object
                    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];