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 app again (of course, NSUserDefaults
are permanent).
Now, the user have the possibility to disable the username/password saving feature.
So the NSUserDefaults
will be cleared then.
But In my app I need this username/password for database queries for the user.
So: Where to store the data except NSUserDefaults
?
(This place can / should be deleted when the user quit the app or logout).
I decided to answer how to use keychain in iOS 8 using Obj-C and ARC.
1)I used the keychainItemWrapper (ARCifief version) from GIST: https://gist.github.com/dhoerl/1170641/download - Add (+copy) the KeychainItemWrapper.h and .m to your project
2) Add the Security framework to your project (check in project > Build phases > Link binary with Libraries)
3) Add the security library (#import ) and KeychainItemWrapper (#import "KeychainItemWrapper.h") to the .h and .m file where you want to use keychain.
4) To save data to keychain:
5) Read data (probably login screen on loading > viewDidLoad):
Enjoy!
There is a small bug in the above code (by the way Dave it was very helpful your post thank you)
In the part where we save the credentials it also needs the following code in order to work properly.
most probably is because the second time we try to (re-)sign in with the same credentials it finds them already assigned in the keychain items and the app crashes. with the above code it works like a charm.