Skip to content

About

πŸ” Production-ready, thread-safe, Sendable Apple Keychain wrapper for iOS & macOS with Codable support and access controls.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

Β 

History

6 Commits

Folders and files

Repository files navigation

KeychainStore πŸ”

CI A lightweight, thread-safe, and Sendable Swift wrapper for Apple's Keychain Services on iOS, macOS, watchOS, and tvOS with built-in Codable serialization and automatic upserting.

Swift Platforms SPM License: MIT

KeychainStore Architecture Diagram


🌟 Highlights

  • ⚑ Swift Concurrency Ready: Conforms to Sendable for seamless usage across async tasks, actors, and background queues.
  • πŸ”„ Automatic Upsert: Smoothly updates existing keys without throwing errSecDuplicateItem errors.
  • πŸ“¦ Native Codable Support: Directly store and retrieve complex structs and data models (store.setModel(user, forKey: "session")).
  • πŸ›‘οΈ Fine-Grained Accessibility: Easily specify item access policies (.afterFirstUnlock, .whenUnlocked, .whenPasscodeSetThisDeviceOnly).
  • πŸ‘₯ App Groups Compatible: Share credentials securely across apps, widgets, and app extensions using accessGroup.

πŸš€ Installation

Add KeychainStore to your dependencies in Package.swift:

dependencies: [
    .package(url: "https://github.com/nilkanthdesai76/swift-keychain-store.git", from: "1.0.0")
]

πŸ’» Quick Start

1. Initialize the Store

import KeychainStore

// Standard store
let keychain = KeychainStore(service: "com.example.myapp")

// Shared App Group store (Widgets, App Extensions)
let sharedKeychain = KeychainStore(
    service: "com.example.myapp",
    accessGroup: "group.com.example.myapp"
)

2. Save and Retrieve Strings

// Save or update
try keychain.setString("secure_access_token_xyz", forKey: "user_token")

// Read
if let token = try keychain.getString(forKey: "user_token") {
    print("Found token: \(token)")
}

// Delete
try keychain.delete(forKey: "user_token")

3. Store Custom Codable Models

struct AuthSession: Codable {
    let userId: String
    let refreshToken: String
    let expiresAt: Date
}

let session = AuthSession(userId: "usr_102", refreshToken: "ref_abc", expiresAt: Date())

// Persist struct directly
try keychain.setModel(session, forKey: "current_session")

// Retrieve struct directly
if let savedSession = try keychain.getModel(AuthSession.self, forKey: "current_session") {
    print("Logged in as user: \(savedSession.userId)")
}

πŸ§ͺ Testing

Run tests via the Swift CLI:

swift test

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

πŸ” Production-ready, thread-safe, Sendable Apple Keychain wrapper for iOS & macOS with Codable support and access controls.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages