I have a login-screen in my iOS app.
The username and password will be saved in the NSUserDefaults
and be loaded into the login-screen again when you enter the
The following should work just fine:
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"YourAppLogin" accessGroup:nil];
[keychainItem setObject:@"password you are saving" forKey:kSecValueData];
[keychainItem setObject:@"username you are saving" forKey:kSecAttrAccount];
To update this question:
For those using Swift checkout this drag and drop swift implementation by Mihai Costea supporting access groups:
https://github.com/macostea/KeychainItemWrapper.swift/blob/master/KeychainItemWrapper.swift
Before using the keychain: consider twice before storing passwords. In many cases storing an authentication token (such as a persistence session id) and the email or account name might be enough. You can easily invalidate authentication tokens to block unauthorized access, requiring the user to login again on the compromised device but not requiring reset password and having to login again on all devices (we are not only using Apple are we?).
For swift you can use this library:
https://github.com/jrendel/SwiftKeychainWrapper
It supports all versions of swift.
checkout this sample code i tried first the apple's wrapper from the sample code but this is much simpler for me
But, now you can go for NURLCredential instead of keychain wrapper. It does what we need to do.
You can simply use NSURLCredential
, it will save both username and password in the keychain in just two lines of code.
See my detailed answer.