iOS: How to store username/password within an app?
You should always use Keychain to store usernames and passwords, and since it's stored securely and only accessible to your app, there is no need to delete it when app quits (if that was your concern).
Apple provides sample code that stores, reads and deletes keychain items and here is how to use the keychain wrapper class from that sample which greatly simplifies using Keychain.
Include Security.framework (in Xcode 3 right-click on frameworks folder and add existing framework. In Xcode 4 select your project, then select target, go to Build Phases tab and click + under Link Binary With Files) and KeychainItemWrapper .h & .m files into your project, #import the .h file wherever you need to use keychain and then create an instance of this class:
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"YourAppLogin" accessGroup:nil];
(YourAppLogin can be anything you chose to call your Keychain item and you can have multiple items if required)
Then you can set the username and password using:
[keychainItem setObject:@"password you are saving" forKey:kSecValueData];
[keychainItem setObject:@"username you are saving" forKey:kSecAttrAccount];
Get them using:
NSString *password = [keychainItem objectForKey:kSecValueData];
NSString *username = [keychainItem objectForKey:kSecAttrAccount];
Or delete them using:
[keychainItem resetKeychainItem];
How to save username and password in an app so that we do not need to login the app?
Use the class NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:@"username" forKey:@"usersave"];
[[NSUserDefaults standardUserDefaults] setObject:@"usernamexxx" forKey:@"passsave"];
[[NSUserDefaults standardUserDefaults] synchronize];
Get them using:
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"passsave"];
NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"usersave"];
How can we store a username-password combination in keychain in encrypted format
I suggest you to store the password, using the login as a key. something like : acccount_test@test.com / password
.
You can encode the md5
value of the passcode to improve security too
Xcode: Store Username and Password
Below are some ways in which you can stored your credential:
NSUserDefault
plist
KeyChain Store
Local Database (SQLite)
CoreData
From above all, NSUserDefault and Keychain are best and easy way to use.
NSUserDefault:
For Storing into Default:
UserDefaults.standard.set(true, forKey: "Key") //Bool
UserDefaults.standard.set(1, forKey: "Key") //Integer
UserDefaults.standard.set("TEST", forKey: "Key") //setObject
Getting from Defaults:
UserDefaults.standard.bool(forKey: "Key")
UserDefaults.standard.integer(forKey: "Key")
UserDefaults.standard.string(forKey: "Key")
KeyChain: . Checkout this demo for keychain.
Proper way to store username and password in iOS application
Apple provides the keychain for storing sensitive information.
https://developer.apple.com/library/ios/documentation/Security/Conceptual/keychainServConcepts/01introduction/introduction.html#//apple_ref/doc/uid/TP30000897
You should not use NSUserDefaults or CoreData unless you have provided some means of encrypting the content, and even so, you'll still need to manage and store encryption keys securely. The keychain provides all of this for you, and with iOS 8 you can now flag keychain items to require presence of a device passcode if desired.
Related Topics
Why Is the Tab Bar Disappearing
How to Fix iOS 12 Safari Cors Preflight Error
What Are Unwind Segues For and How to Use Them
Vertically Align Text to Top Within a Uilabel
Fatal Error: Unexpectedly Found Nil While Unwrapping an Optional Values
How to Make a Simple Collection View With Swift
How to Parse a Json File in Swift
Uiview Infinite 360 Degree Rotation Animation
Why Isn't Projectname-Prefix.Pch Created Automatically in Xcode 6
Measuring Cellular Signal Strength
How to Check How Long Ago Was the App Last Opened
How to Make a Uitextfield Move Up When the Keyboard Is Present - on Starting to Edit
Converting Nsstring to Nsdate (And Back Again)
Nsfilemanager.Defaultmanager().Fileexistsatpath Returns False Instead of True
Setting Action For Back Button in Navigation Controller