E2EHIRING Logo
Jobs
Jobs
courses
Courses
mentorship
Mentorship
more
Moredropdown
E2EHIRING Logo
more
Jobs
Jobs
courses
Courses
mentorship
Mentorship
HomeSepratorIconBlogsSepratorIconWhat is the Keychain in iOS & How to store data in keychainSepratorIcon

What is the Keychain in iOS & How to store data in keychain

Han SoloUday Kc
calendar18 Jul 2022
poster

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

  • Keychain: The keychain is a secure and encrypted storage place for sensitive data. You can think of it is a database of sensitive information.
  • Keychain Item: This is a registry in the keychain.
  • Item Class: You can think of a class as a template of information you want to store. The keychain offers classes for different common credentials, such as username/password pairs, a certificate, a generic password, and more.   

Implementation of Keychain for storing,retrieving and deleting data 

You can use Swift Package Manager to install SwiftKeychainWrapper using Xcode:

  1. Open your project in Xcode
  2. Click "File" -> "Swift Packages" -> "Add Package Dependency". 
  3. Paste the following URL: https://github.com/jrendel/SwiftKeychainWrapper
  4. Click "Next" -> "Next" -> "Finish"  

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()

Reference: https://developer.apple.com/documentation/security/certificate_key_and_trust_services/keys/storing_keys_in_the_keychain

That's all folks!

Recent Posts

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

How to publish your Android app on Google Play Store

How to publish your Android app on Google Play Store

Creating Dynamic User Interfaces with Android Motion Layout

Creating Dynamic User Interfaces with Android Motion Layout

Bean Life Cycle

Bean Life Cycle

Pom.XML

Pom.XML

copycopycopycopy

Han Solo

Recent Posts

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

How to publish your Android app on Google Play Store

How to publish your Android app on Google Play Store

Creating Dynamic User Interfaces with Android Motion Layout

Creating Dynamic User Interfaces with Android Motion Layout

Bean Life Cycle

Bean Life Cycle

Pom.XML

Pom.XML