Skip to content

A Swift API for storing, retrieving, and removing generic password items in the system Keychain.

License

Notifications You must be signed in to change notification settings

Electrode-iOS/ELKeychain

Repository files navigation

ELKeychain

Build Status Carthage Compatible

A Swift framework for storing, retrieving, and removing generic password items in the system Keychain.

Requirements

ELKeychain requires Swift 5 and Xcode 10.2.

Installation

Carthage

Install with Carthage by adding the framework to your project's Cartfile.

github "Electrode-iOS/ELKeychain"

Manual

Install manually by adding ELKeychain.xcodeproj to your project and configuring your target to link ELKeychain.framework.

There are two target that builds ELKeychain.framework.

  1. ELKeychain: Creates dynamically linked ELKeychain.framework.
  2. ELKeychain_static: Creates statically linked ELKeychain.framework.

Both targets build the same product (ELKeychain.framework), thus linking the same app against both ELKeychain and ELKeychain_static should be avoided.

Usage

Storing a Generic Password Item

let password = "12345"

do {
  try Keychain.set(password, account: "king-roland", service: "druidia-airshield")
} catch let error {
    // failed to store keychain item
}

Retrieving a Generic Password Item

do {
    if let password: String = try Keychain.get(account: "king-roland", service: "druidia-airshield") {
        Airshield.unlock(password: password)
    } else {
      // unable to find password to unlock
    }
} catch let error {
    // an error occurred while retrieving the keychain item
}


Removing a Generic Password Item

do {
    try Keychain.delete(account: "king-roland", service: "druidia-airshield")
} catch let error {
    // an error occurred while trying to delete the keychain item
}

Storing a Generic Password Item w/ Access Control

do {
    let accessControl = try AccessControl(protection: .whenPasscodeSetThisDeviceOnly, policy: .UserPresence)
    try Credential.keychain.set("12345", account: "king-roland", accessControl: accessControl)
} catch let error {
    // an error occurred
}