Skip to content

woin2ee/Keychaining

Repository files navigation

Keychaining

Version License Platform

Keychaining is wrapper for keychain that is similar to how you would use Keychain without any libraries.

Usage

Import

import Keychaining

Synchronous Basic

Save

do {
    try Keychain.genericPassword.makeSaveQuery()
        .setService("Keychaining")
        .setAccount("Account")
        .setDataFor("Private Data")
        .execute()
} catch {
    print(error)
}

Search

do {
    let data: Data = try Keychain.genericPassword.makeSearchQuery()
        .setService("Keychaining")
        .setAccount("Account")
        .setReturnTypes(.data)
        .execute()
    print(data)
} catch {
    print(error)
}

Delete

do {
    try Keychain.genericPassword.makeDeleteQuery()
        .setService("Keychaining")
        .setAccount("Account")
        .execute()
} catch {
    print(error)
}

Use Asynchronous (*recommended)

Task {
    try await Keychain.genericPassword.makeSaveQuery()
        .setService("Keychaining")
        .setAccount("Account")
        .setDataFor("Private Data")
        .execute()
}

Simple Use

Save

try Keychain.set("Some string", forKey: "key")

Get

let data: Data = try Keychain.getData(forKey: "key")

or

let string: String = try Keychain.getString(forKey: "key")

Delete

try Keychain.delete(forKey: "key")

Advanced

Set any attributes

let query = Keychain.internetPassword.makeBasicQuery()
    .setAccessGroup(accessGroup)
    .setAccessible(accessible)
    .setCreationDate(creationDate)
    .setModificationDate(modificationDate)
    .setDescription(description)
    .setComment(comment)
    .setCreator(creator)
    .setType(type)
    .setLabel(label)
    .setInvisible(isInvisible)
    .setNegative(isNegative)
    .setAccount(account)
    .setSecurityDomain(securityDomain)
    .setServer(server)
    .setProtocol(`protocol`)
    .setAuthenticationType(authenticationType)
    .setPort(port)
    .setPath(path)
    .setSynchronizable(synchronizable)

Reuse query

let defaultQuery = Keychain.genericPassword.makeBasicQuery()
    .setService(service)
    .setAccount(account)
    
try defaultQuery.forSave
    .setLabel(label)
    .setData(passwordData)
    .execute()
    
let data: Data = try defaultQuery.forSearch
    .setLabel(label)
    .setReturnTypes(.data)
    .execute()

Specific error handling

do {
    let data = try Keychain.genericPassword.makeSearchQuery()
        .setAccount("None")
        .execute()
        ...
} catch {
    if error.asKeychainError == .itemNotFound {
        ...
    }
    if let errorMessage = error.asKeychainError?.errorMessage {
        print(errorMessage)
    }
}

Requirements

  • iOS 12.4+
  • Xcode 13.3+
  • Swift 5.6+

Installation

Keychaining is available through CocoaPods. To install it, simply add the following line to your Podfile:

use_frameworks!

target 'target_name' do
   pod 'Keychaining'
end

Author

Jaewon Yun, woin2ee@gmail.com

License

Keychaining is available under the MIT license. See the LICENSE file for more info.

About

Use keychain as a method chain pattern on iOS.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published