2

I am implementing an application, the first view is the login view. it contains 3 textfields Account name, Username and password. i want to let the application to save the login information in order not to let the user write them each time he/she opens the application. And to be deleted when he/she logs out.

how to do that? and how to read/write on a file?

Thankfully yours

fadd
  • 584
  • 4
  • 16
  • 41
  • 4
    Please dont store sensitive data in NSUserDefaults. Either use KeychainService or first encrypt data after store it in NSUserDefault. – Iducool Sep 10 '12 at 10:58
  • @fadddd I don't advise to store sensitive data into `NSUserDefaults`, KeychainItemWrapper is the best option you can have. Good luck. – viral Sep 10 '12 at 11:07

4 Answers4

28

Use key chain for storing login password. Below is the simple code

To store:

KeychainItemWrapper *keychain = 
 [[KeychainItemWrapper alloc] initWithIdentifier:@"MyAppLoginData" accessGroup:nil];
[keychain setObject:loginStr forKey:(id)kSecAttrAccount];
[keychain setObject:pwdStr forKey:(id)kSecValueData];

To query:

NSString *login = [keychain objectForKey:(id)kSecAttrAccount];
NSString *pwd = [keychain objectForKey:(id)kSecValueData];

To Delete:

[keychain resetKeychainItem];

You need to add KeychainItemWrapper.h and KeychainItemWrapper.m (here) in your project first.

Another important aspects of using keychain to store data is

  1. The data is persistent even after app uninstall-install
  2. The data can be shared across your apps (need to have same bundle seed id, read from here). Think of single sign on for all your apps.
  3. The data is removed only on Device Reset from settings.
msk
  • 8,885
  • 6
  • 41
  • 72
  • well how about deleting it ? – Elgert Jul 07 '14 at 13:03
  • @msk Does this just store it in Keychain or does it encrypt and then store it in Keychain? Also lets say you make a password like "hello". And you encrypt/store it in Keychain, how do you get the encrypted string back? SO lets say "rw135tq52r" is encryption string for "hello". How do you get "rw135tq52r" from keychain? – Supertecnoboff Feb 24 '15 at 22:09
  • 1
    From Apple documentation "All the password data in the keychain is protected using the Triple Digital Encryption Standard (3DES)." – msk Feb 25 '15 at 17:01
2

This kind of sensitive data is usually stored in keychain. Similar question here

Community
  • 1
  • 1
Templar
  • 1,694
  • 1
  • 14
  • 32
2

Check keychainServConcepts for saving required data

Check STUtils for saving Username and password securely.

U can also use NSUserDefault to save data with key as userName and remove when required.

Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
-7
To Save:

    [[NSUserDefaults standardUserDefaults] setValue:AccountTxtField.text forKey:@"Account"];
    [[NSUserDefaults standardUserDefaults] setValue:UserTxtField.text forKey:@"Username"];
    [[NSUserDefaults standardUserDefaults] setValue:passwordTxtField.text forKey:@"password"];
    [[NSUserDefaults standardUserDefaults] synchronize];

To Read:

    NSString * _Account = [[NSUserDefaults standardUserDefaults] stringForKey:@"Account"];
    NSString * _UserName = [[NSUserDefaults standardUserDefaults] stringForKey:@"Username"];
    NSString * _password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"];

logOut : set all value is null.

Vikas S Singh
  • 1,748
  • 1
  • 14
  • 29
  • 15
    BAD advice. Don't use user defaults to store sensitive data. As others have already answered, use the keychain API. – Jens Kilian Sep 10 '12 at 13:45
  • @JensKilian please read and review the Questions , then comments – Vikas S Singh Sep 11 '12 at 04:08
  • i know it is unsecured at all, but it is the easiest and the faster. and my data are not that secure. Thanks all for the advices :) – fadd Sep 11 '12 at 08:43
  • 3
    How on earth "login credentials" are not considering as that secure! The right way is keychain but yeah you can create something somewhere and save it there for later on! – Maziyar May 28 '14 at 06:52
  • @Maziyar This one is a Quick method for save the credentials, if you want save your credentials as a secure then you save the keychain, this is the best option for secure. – Vikas S Singh May 28 '14 at 08:06
  • I guess you could use NSUserDefaults as long as you encrypt the password string first before storing it?? – Supertecnoboff Feb 24 '15 at 21:17
  • 1
    @RajputVikasSingh if the answer is proposed as "quick & temporary & not secure", you should state that in uppercase as the first line of your answer. So that it does not mislead other people, for whom user credentials normally ARE sensitive. – user1244109 Oct 05 '15 at 22:19