Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving login credentials on iOS

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 save the login information in order not to let the user write them each time they open the application. And to be deleted when they log out.

How to do that? And how to read/write on a file?

like image 570
fadd Avatar asked Dec 07 '25 08:12

fadd


2 Answers

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.
like image 149
msk Avatar answered Dec 08 '25 21:12

msk


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

like image 36
Templar Avatar answered Dec 08 '25 21:12

Templar