Keychaining is wrapper for keychain that is similar to how you would use Keychain without any libraries.
import Keychaining
do {
try Keychain.genericPassword.makeSaveQuery()
.setService("Keychaining")
.setAccount("Account")
.setDataFor("Private Data")
.execute()
} catch {
print(error)
}
do {
let data: Data = try Keychain.genericPassword.makeSearchQuery()
.setService("Keychaining")
.setAccount("Account")
.setReturnTypes(.data)
.execute()
print(data)
} catch {
print(error)
}
do {
try Keychain.genericPassword.makeDeleteQuery()
.setService("Keychaining")
.setAccount("Account")
.execute()
} catch {
print(error)
}
Task {
try await Keychain.genericPassword.makeSaveQuery()
.setService("Keychaining")
.setAccount("Account")
.setDataFor("Private Data")
.execute()
}
try Keychain.set("Some string", forKey: "key")
let data: Data = try Keychain.getData(forKey: "key")
or
let string: String = try Keychain.getString(forKey: "key")
try Keychain.delete(forKey: "key")
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)
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()
do {
let data = try Keychain.genericPassword.makeSearchQuery()
.setAccount("None")
.execute()
...
} catch {
if error.asKeychainError == .itemNotFound {
...
}
if let errorMessage = error.asKeychainError?.errorMessage {
print(errorMessage)
}
}
- iOS 12.4+
- Xcode 13.3+
- Swift 5.6+
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
Jaewon Yun, woin2ee@gmail.com
Keychaining is available under the MIT license. See the LICENSE file for more info.