KeyChainItemCRUDKit 0.0.3

KeyChainItemCRUDKit 0.0.3

LangLanguage SwiftSwift
License MIT
ReleasedLast Release Nov 2015
SPMSupports SPM

Maintained by Pete Barber.


A simple Swift library to CRUD instances of NSData and/or objects implementing NSCoding into the iOS KeyChain


  • Simple library for Saving, Loading & Deleting NSCoding compatable objects into the iOS Keychain (single class)
  • Full Create, Read, Update & Delete semantics if required
  • In Swift
  • Available as a CocoaPod


There are 2 interfaces to this library

  • KeyChainItemCRUD
  • KeyChainItem

Both classes are constructed using 2 values: the BundleId of your application (though it can be any string in practice) and a unique key (also a string) to identify your item.


let itemCRUDer = KeyChainItemCRUD(bundleId: NSBundle.mainBundle().bundleIdentifier!, keyChainItemKey: “something”)

then the methods

  • createItem(data: NSData) -> Bool
  • readItem() -> NSData?
  • updateItem(data: NSData) -> Bool
  • deleteItem() -> Bool

can be called

The major difference between these methods and the higher level ones following is that each of these is specific about what it does. createItem() will only create an item if it doesn’t already exist. It will return false it does. Likewise, updateItem() require an existing item. Obviously so does readItem().

In addition to return a value indicating success or failure the property lastError is set with the return value of the underlying KeyChain API


For a lot of intents and purposes it’s irrelevent when saving an item whether this is creating it or updating it. Likewise deleting a non-existant item doesn’t usually matter. If this is the case then use these higher level methods. Initially create an instance of KeyChainItemKey

let item = KeyChainItem(bundleId: NSBundle.mainBundle().bundleIdentifier!, keyChainItemKey: “something”)

then the methods

  • load() -> NSData?
  • save(data: NSData) -> Bool

or ideally if the item that needs loading/saving is NSCoding compliant:

  • load() -> T?
  • save(t: T) -> Bool


Multiple items can be CRUDed but as each will require a different key then different instances will be required.


The minimum target is iOS 8. The source code could be copied into a separate project but it is packaged as Framework


An example application is included. It follows the CocoaPods standard of being called Example. It’s a simple SingleView applications that allows a single item to be created, read, updated & deleted. It uses the higher level object interface.


Unit tests for both the CRUD and high level interface are provided.