Keychain offers a secure alternative to saving sensitive data, such as
user names and passwords, with NSUserDefaults, plist or similar
methods.
As you might already know, NSUserDefaults is simple and effective for
saving small, simple bits of data, like NSNumbers or NSStrings, to
your device’s file system. But this data is in no way stored securely
as hackers can access it pretty easily from the device.
Apple has provided the Keychain Services API to deal with this problem
and help developers build apps that safely handle passwords and other
sensitive information.
A keychain is defined in Apple’s documentation as:
Keychain is great because data encryption automatically is taken care
of before it is stored in the file system so there is no need to waste
time building encryption algorithms.
A keychain in both OS and iOS can be configured to lock. When locked
it is impossible to access and decrypt stored keychain items. For iOS
the keychain is locked when the device is locked and unlocked when the
device is unlocked. Even when it is unlocked, only apps that have
created an item can access it, unless configured otherwise.
Keychain also offers other features like:
Accessing keychain items
across apps. Normally, an app only has access to items it created but
configuration can be made to let it access data within a group of
designated apps.
Securing user data with Keychain for iOS
Use
Apple’s own Keychain wrapper is called GenericKeychain and is available within the sample code in both Objective C and Swift.
Here are a few Keychain wrappers I recommend:
SwiftKeychainWrapper by Jason Rendel(jrendel)
SAMKeychain by Sam Soffes for Objective C.
Locksmith by Matthew Palmer for Swift. (Check out the video tutorial)