Latest0.8.1
Homepagehttps://github.com/toddkramer/CodableKeychain
LicenseMIT
Platformsios 8.0, osx 10.10, tvos 9.0, watchos 2.0

CocoaPods Version Swift Platform Swift Package Manager compatible Carthage compatible

Overview

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

Features

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

Usage

KeychainStorable

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:

Keychain.resetDefaults()

Storing

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

Retrieving

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

Installation

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

Targets using CodableKeychain must support embedded Swift frameworks.

CocoaPods

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:
use_frameworks!

pod 'CodableKeychain', '~> 0.8.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
Manager:

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

let package = Package(
    name: "MyAppTarget",
    dependencies: [
        .Package(url: "https://github.com/toddkramer/CodableKeychain", majorVersion: 0, minor: 7)
    ]
)
  1. Run swift build.

  2. Generate Xcode project:
swift package generate-xcodeproj

Carthage

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.8.1
  1. Run carthage update and add the appropriate framework.

Latest podspec

{
    "name": "CodableKeychain",
    "version": "0.8.1",
    "license": "MIT",
    "summary": "Swift framework for storing Codable conforming objects to the keychain.",
    "homepage": "https://github.com/toddkramer/CodableKeychain",
    "social_media_url": "http://twitter.com/_toddkramer",
    "authors": "Todd Kramer",
    "source": {
        "git": "https://github.com/toddkramer/CodableKeychain.git",
        "tag": "0.8.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.1"
}

Pin It on Pinterest

Share This