What is KeychainAccess and Why Do You Need It?
KeychainAccess is a lightweight yet powerful Swift library that provides a convenient, type-safe interface for working with Apple's Keychain Services. The Keychain is a secure storage system built into iOS, macOS, watchOS, and tvOS, designed to store sensitive data: passwords, encryption keys, authentication tokens, credit card numbers, and any other confidential information.
Without libraries, working with the Keychain through the native C interface (functions like SecItemAdd, SecItemCopyMatching, SecItemUpdate, SecItemDelete) is extremely cumbersome, requires writing a lot of boilerplate code, and is error-prone. KeychainAccess solves this problem by offering:
- Simple Swift syntax: work with properties (get/set) and methods with clear, descriptive names.
- Type safety: data is automatically converted to
String,Data, or custom types that conform toCodable. - Flexibility: support for various access levels (app availability, after device unlock, with biometrics), Keychain groups, and iCloud synchronization.
- Security by default: the library follows Apple's best security practices out of the box.
This library is ideal for storing OAuth tokens, user passwords, API keys, crypto wallet seed phrases, and any other data that should never be stored in UserDefaults or the file system in plain text.
Installing KeychainAccess
KeychainAccess is not installed via pip (Python) or npm (JavaScript). It's a native library for the Apple ecosystem. Here are the main installation methods:
1. Swift Package Manager (SPM) — Recommended
In Xcode: File → Add Packages... → paste the repository URL:
https://github.com/kishikawakatsumi/KeychainAccess.git
Select a version (usually "Up to Next Major" with the latest stable release) and add the package to your target.
Or add the dependency to your Package.swift file:
// swift-tools-version:5.7
import PackageDescription
let package = Package(
name: "MyPackage",
dependencies: [
.package(url: "https://github.com/kishikawakatsumi/KeychainAccess.git", from: "4.2.2")
],
targets: [
.target(
name: "MyPackage",
dependencies: ["KeychainAccess"]),
]
)
2. CocoaPods
Add this line to your Podfile:
pod 'KeychainAccess'
Then run pod install.
3. Carthage
Add to your Cartfile:
github "kishikawakatsumi/KeychainAccess"
Then run carthage update and add the framework to your project.
Quick Start with KeychainAccess
After installation, import the library: import KeychainAccess
Here's the simplest example — saving and reading a string:
import KeychainAccess
// Create an instance to work with the app's Keychain
let keychain = Keychain(service: "com.example.myapp")
// Save a value
try keychain.set("my_secret_password", key: "user_password")
// Read a value
if let password = try keychain.get("user_password") {
print("Found password: \(password)")
} else {
print("Password not found")
}
// Delete a value
try keychain.remove("user_password")
This code performs three basic operations: write, read, and delete. Note the error handling with try — Keychain operations can throw exceptions (for example, due to access issues or missing permissions).
Key Methods and Functions in KeychainAccess
Let's explore