
What is the Keychain
The keychain is the best place to store small secrets, like passwords and cryptographic keys. You use the functions of the keychain services API to add, retrieve, delete, or modify keychain items.
Apple Inc. uses keychains as a password management system in Mac OS and iOS. Apple recommends storing only a small amount of data in the Keychain. If you need to secure something big you can encrypt it manually, save to a file and store the key in the Keychain.
Basic Keychain Concepts
Implementation of Keychain for storing,retrieving and deleting data
You can use Swift Package Manager to install SwiftKeychainWrapper using Xcode:
OR
Install SwiftKeychainWrapper via podfile by adding pod 'SwiftKeychainWrapper' to your pod file.
SwiftKeychainWrapper
It provides a singleton instance that is set up to work for most needs. Use KeychainWrapper.standard to access the singleton instance.
To use this wrapper you need to import it in your project. For import write import SwiftKeychainWrapper in your project file.
Usage
Here an example of storing value in a keychain is given below
KeychainWrapper.standard.set(“u[email protected]”, forKey: “email”)
Here an example of how to retrieve a stored value in a keychain is given below
let email = KeychainWrapper.standard.string(forKey: "email")
Here is an example of deleting string values from the keychain
KeychainWrapper.standard.removeObject(forKey: "email")
Here is an example of deleting all stored values from the keychain
KeychainWrapper.standard.removeAllKeys()
That's all folks!