Latest 0.7.1
License MIT
Platforms ios 8.0, osx 10.10, tvos 9.0, watchos 2.0

CocoaPods Version Swift Platform Swift Package Manager compatible Carthage compatible


CodableKeychain is a Swift framework for saving objects conforming to the Codable protocol to the keychain.


  • iOS 8.0+, macOS 10.10+, tvOS 9.0+, watchOS 2.0+
  • 100% Test Coverage
  • Easily store any object conforming to Codable
  • Specify item accessibility using a Swift enum (see Keychain.AccessibleOption)
  • Converts the 35 documented keychain error codes into a Swift enum for easy error handling (see KeychainError)



The KeychainStorable protocol, which inherits from Codable, is used to define a model that can be stored using CodableKeychain.

Note: If you do not define a service or access group, CodableKeychain will default to your app’s bundle identifier for the service and nil for the access group.

import CodableKeychain

enum Constants {
    static let service = "com.example.credentialservice"
    static let accessGroup = "[APP_ID_PREFIX].com.example.TestKeychain"

struct Credential: KeychainStorable {
    let email: String
    let password: String
    let pin: Int
    let dob: Date

extension Credential {

    var account: String { return email }
    var accessible: Keychain.AccessibleOption { return .whenPasscodeSetThisDeviceOnly }


Configuring Defaults

If the service and access group will be the same for all keychain operations, use the following to configure default values:

Keychain.configureDefaults(withService: Constants.service, accessGroup: Constants.accessGroup)

To reset the defaults:



let credential = Credential(email: "[email protected]", password: "foobar", pin: 1234, dob: Date(timeIntervalSince1970: 100000))
do {
} catch let error {


do {
    let credential: Credential? = try Keychain.default.retrieveValue(forAccount: "[email protected]")
} catch let error {


Note: CodableKeychain requires Swift 4 (and Xcode 9) or greater.

Targets using CodableKeychain must support embedded Swift frameworks.


CocoaPods is a centralized dependency manager for Cocoa projects. To install
CodableKeychain with CocoaPods:

  1. Make sure the latest version of CocoaPods is installed.

  2. Add CodableKeychain to your Podfile:

pod 'CodableKeychain', '~> 0.7.1'
  1. Run pod install.

Swift Package Manager

Swift Package Manager is Apple’s
official package manager for Swift frameworks. To install with Swift Package

  1. Add CodableKeychain to your Package.swift file:
import PackageDescription

let package = Package(
    name: "MyAppTarget",
    dependencies: [
        .Package(url: "", majorVersion: 0, minor: 7)
  1. Run swift build.

  2. Generate Xcode project:
swift package generate-xcodeproj


Carthage is a decentralized dependency manager for Cocoa projects. To
install CodableKeychain with Carthage:

  1. Make sure Carthage is installed.

  2. Add CodableKeychain to your Cartfile:
github "toddkramer/CodableKeychain" ~> 0.7.1
  1. Run carthage update and add the appropriate framework.

Latest podspec

    "name": "CodableKeychain",
    "version": "0.7.1",
    "license": "MIT",
    "summary": "Swift framework for storing Codable conforming objects to the keychain.",
    "homepage": "",
    "social_media_url": "",
    "authors": "Todd Kramer",
    "source": {
        "git": "",
        "tag": "0.7.1"
    "module_name": "CodableKeychain",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "tvos": "9.0",
        "watchos": "2.0"
    "source_files": "Sources/*.swift",
    "pushed_with_swift_version": "4.0"

Pin It on Pinterest

Share This