Friday, November 30, 2012

How to store values in keychain of ios

How to store values in keychain of ios

Get contacts of all your emergency needs on single tap. Download Quick-Finder for iPhone

Tutorial for: Key chain, user defaults, values, KeychainItemWrapper, iphone, ios

What is keyChain -

Key chain is the place where we can store the values which will be retain even after deleting application. Generally we store username and password in it.


Difference between KeyChain and userDefaults
The values which are store in userDeafults are deleted when we delete the app from our device but the values stored in KeyChain will not.


Apple provides helper classes, for that you can refer to this link
htps://developer.apple.com/library/ios/#samplecode/GenericKeychain/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007797
Or you can download it from here

Initialise the class

KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"Your application name" accessGroup:nil];

How to save the values
[wrapper setObject:@"PASSWORD" forKey:@"KEYPASS"];
[wrapper setObject:@"USERNAME" forKey:@"KEYUSER"];


how to retrive the values

NSString *pwd = [wrapper objectForKey:@"KEYPASS"];
NSString *user = [wrapperobjectForKey:@"KEYUSER"];


How to delete the values

[wrapper resetKeychainItem];

Reference: http://stackoverflow.com/questions/6972092/ios-how-to-store-username-password-within-an-app
www.Hypersmash.com

9 comments:

  1. I did not working for me.. :(

    ReplyDelete
  2. I've got as far as adding the KeychainItemWrapper class. When I add KeychainItemWrapper.m into my project I get al lsorts of error about incompatible pointer types.

    Project is targeting iOS 6.

    ReplyDelete
  3. Yeah this code seems to be really outdated.. lots of release statements. It's annoying that apple doesn't update it. Lot's of errors, some asking me to insert (__bridge) statements, others saying incompatible pointer types..

    I found this code (https://gist.github.com/dhoerl/1170641) which has no errors (for me) but I can't figure out how to make it work still... when I call something like [wrapper setObject:@"USERNAME" forKey:@"KEYUSER"];

    it doesn't work...

    ReplyDelete
  4. Thank you for your guide to with upgrade information.
    iOS Online Training Bangalore

    ReplyDelete