KeychainAccess is a simple Swift wrapper for Keychain that works on iOS and OS X. It makes using Keychain APIs extremely easier and much more comfortable using in Swift.
To run the example project, clone the repo, and run pod install
from the Example directory first.
- iOS 9.0+ / macOS 10.15+
- Xcode 11+
- Swift 5.1+
SecureKeychain is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SecureKeychain'
var keychain = KeychainItemGenericPassword(service: "keychain.test")
keychain["key"] = "password"
var keychain = KeychainItemInternetPassword(server: "https://github.com")
keychain["key"] = "3yh3dfgt-s55d-6sf3-rt33-feserte4345t"
var keychain = KeychainItemGenericPassword(service: "keychain.test")
let secureKeychain = SecureKeychain(keychain: KeychainItemGenericPassword(service: "keychain.test"), accessibility: .whenUnlocked, authenticationPolicy: .biometryAny)
var keychain = KeychainItemInternetPassword(server: "https://github.com")
let secureKeychain = SecureKeychain(keychain: KeychainItemInternetPassword(server: "https://github.com"), accessibility: .whenUnlocked, authenticationPolicy: .biometryAny)
do {
try secureKeychain.storeToPasswordProtectedKeychain(value: "Value to store in keychain", for: "PasswordProtectedKey", with: "Password")
} catch {
print("Error while saving to password protected keychain")
return
}
do {
try secureKeychain.storeToBiometricProtectedKeychain(value: "Value to store in keychain", for: "BiometricKey")
} catch {
print("Error while saving to biometric protected keychain")
return
}
keychain["key"] = "3yh3dfgt-s55d-6sf3-rt33-feserte4345t"
let accessibility: KeychainItemAccessibility = .whenUnlocked
let accessControl: [KeychainAccessControlViewModel] = [.biometryAny]
let accessLevel: KeychainItemAccessLevel = (accessibility, accessControl.map { $0.value }, nil)
do {
try keychain.set("password", for: "login", with: accessLevel)
} catch {
print(error)
}
let token = keychain["key"]
let token = try? keychain.get(loginTextField.text!, with: nil, for: nil)
let allKeys = keychain.allKeys
let password = secureKeychain.restorePasswordProtectedValue(for: "PasswordProtectedKey", with: "Password")
secureKeychain.restoreFromEncryptedKeychain(for: "BiometricKey") { value in
print(value)
}
keychain["key"] = nil
keychain.clear()
secureKeychain.clearKey("PasswordProtectedKey")
secureKeychain.clearKey("BiometricKey")
SecureKeychain is available under the MIT license. See the LICENSE file for more info.