How to use Keychain in ios programming [closed]
Asked Answered
H

4

6

I'm using the Keychain wrapper class provided by Apple. In my application I want to store and retrieve the username and password of the logged in user. When the app is launched again, it should check for the login credentials in the keychain and let the user into the app(automatic login). Tried some sample codes. nothing worked out. Please help me out with the procedure to do this.

Thanks.

Hoosegow answered 26/2, 2013 at 10:35 Comment(2)
What have you tried?Austine
Take a look at this library -> github.com/carlbrown/PDKeychainBindingsControllerAlderney
C
16

Use PDKeyChainBindings

This is how to use:

PDKeychainBindings *bindings = [PDKeychainBindings sharedKeychainBindings];
[bindings setObject:@"myuser" forKey:@"username"];
[bindings setObject:@"kmypass" forKey:@"password"];

//How to retrieve
NSString *username = [bindings objectForKey:@"username"];
NSString *password = [bindings objectForKey:@"password"];
Cosmotron answered 26/2, 2013 at 11:17 Comment(0)
S
6

you can save your credential info more securely using keychain access. For that please follow bellow guides and Samples.

1) Keychain Services Programming Guide

You found all as your want in this document by apple.

2) GenericKeychain

This sample code by apple shows how to add, remove and update keychain values.

Let me know if you have any problem in this. Hope this one helpful to you.

Savino answered 26/2, 2013 at 11:17 Comment(0)
A
1
#import "SFHFKeychainUtils.h"
#define KEYCHAIN_SERVICE_KEY   @"MyKey"
#define KEYCHAIN_ID @"agsfs46edt3g3jd38jh3g3g3g3g38f3dkiuytr453"
#define KEYCHAIN_PSD @"a1s2s3d4edt3g3jd38jh3g3g3g3g38f3dfgffgdd"

//to check

NSError *error = nil;
    NSString *password = [SFHFKeychainUtils getPasswordForUsername:KEYCHAIN_ID andServiceName:KEYCHAIN_SERVICE_KEY error:&error];

    if ([password isEqualToString:KEYCHAIN_PSD])
        return YES;

//to save

NSError *error = nil;
        [SFHFKeychainUtils storeUsername:KEYCHAIN_ID  andPassword:KEYCHAIN_PSD forServiceName:KEYCHAIN_SERVICE_KEY updateExisting:YES error:&error];
Argos answered 26/2, 2013 at 10:40 Comment(0)
J
0

Have a look at ACSimpleKeychain. It provides a simple abstraction of the Keychain to allow you to easily save passwords securely.

For example, saving a username and password:

if ([keychain storeUsername:@"user1" password:@"password" identifier:@"account1" forService:@"twitter"])
{
    NSLog(@"SAVED credentials for username 'user1' credentials identifier 'account1'");
}

Retrieving the password:

NSDictionary *credentials = [keychain credentialsForUsername:@"user1" service:@"twitter"];
NSLog(@"CREDENTIALS: service: %@, identifier: %@, username: %@, password: %@",
    [credentials valueForKey:ACKeychainService],
    [credentials valueForKey:ACKeychainIdentifier],
    [credentials valueForKey:ACKeychainUsername],
    [credentials valueForKey:ACKeychainPassword]);
Jurkoic answered 26/2, 2013 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.