Skip to content

radude89/KeychainStorage

Repository files navigation

Keychain Storage

build Swift Package Manager Carthage Carthage iOS Twitter: @radude89

Keychain Storage is a simple Keychain wrapper written in Swift.
If you ever wanted to save something in Keychain without writing too much code, you are in the right place.

Requirements

  • iOS 12.0+
  • Xcode 10.2+
  • Swift 5+

Installation

Cocoa Pods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate Keychain Storage into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'KeychainStorage', '~> 1.3.0'

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate KeychainStorage into your Xcode project using Carthage, specify it in your Cartfile:

github "radude89/KeychainStorage" "1.3.0"

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, you can add KeychainStorage as a dependency by adding it to the dependencies array of your Package.swift.

dependencies: [
    .package(url: "https://github.com/radude89/KeychainStorage.git", from: "1.0.0")
]

Manual

Just drag and drop into your project the following projects found in KeychainStorage:

  • KeychainQueryFactory.swift
  • KeychainStorage.swift
  • KeyValueStorage.swift

Examples

Make sure you try out the small app, the example project found at: Example/KeychainStorageExample.xcworkspace.
Build & run.

Saving a value in Keychain

import KeychainStorage

let storage = KeychainStorage(service: "com.test.service")
try? storage.set("secret", key: "myStringKey")

try? storage.set(true, key: "myBoolKey")

Retrieving a value from Keychain

import KeychainStorage

let storage = KeychainStorage(service: "com.test.service")

if let myStringValue = try? storage.string(forKey: "myStringKey") {
    // do something with myStringValue
}

if let myBoolValue = try? storage.bool(forKey: "myBoolKey") {
    // do something with myBoolValue
}

Deleting a value from Keychain

import KeychainStorage

let storage = KeychainStorage(service: "com.test.service")
try? storage.removeValue(forKey: "myStringKey")

Deleting all values from Keychain

import KeychainStorage

let storage = KeychainStorage(service: "com.test.service")
try? storage.removeAll()

License

KeychainStorage is released under the MIT license. See LICENSE for details.

Contributions & support

KeychainStorage is developed as an open source project. I encourage everyone to contribute.

Please do make pull requests if you have suggestions or ideas of improvement.

Thanks!