To run the example project, clone the repo, and run pod install from the Example directory first.
TCBKeychainManager is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod'TCBKeychainManager'If pods path is not found, You can either add: source 'https://github.com/CocoaPods/Specs.git' in your Podfile. Or use this format:
# This will always pull the latest. You'll not be able to set a specific versionpod'TCBKeychainManager',:git=>'https://github.com/TheCodingBug/TCBKeychainManager.git'Sample usage of class managers for specific purposes, nescessary config changes are expected from integrators:
Sample constructor for adding an Item to the Keychain.
varmanager=TCBKeychainStoreAddManager<TCBKeychainStoreItemAttributeGenericPassword>(itemType:.genericPassword)do{letpasswordData="test_account_password".data(using:.utf8)!
try manager.addValue(for:.account, value:"test_account")try manager.addValue(for:.password, value: passwordData)letaddResult=try manager.saveItem()print("\nAdd Status: \(addResult.description!)")}catch{iflet error = error as?TCBKeychainStoreStatus{print("manager: \(error.description!)")}}Sample constructor for updating an Item from the Keychain.
varmanager=TCBKeychainStoreUpdateManager<TCBKeychainStoreItemAttributeGenericPassword,TCBKeychainStoreItemAttributeSearchQuery>(itemType:.genericPassword)do{letpassword="test_account_password_\(Int.random(in:0..<9999))"letpasswordData= password.data(using:.utf8)!
try manager.addQuery(for:.account, value:"test_account")try manager.addValueToUpdate(for:.password, value: passwordData)letupdateResult=try manager.updateItem()print("\n\nUpdate Result: \(updateResult.description!)")}catch{iflet error = error as?TCBKeychainStoreStatus{print("\n\n manager: \(error.description!)")}}varmanager=TCBKeychainStoreSearchManager<TCBKeychainStoreItemAttributeGenericPassword,TCBKeychainStoreItemAttributeSearchQuery>(itemType:.genericPassword)do{try manager.addSearchValue(for:.account, value:"test_account")
// `TCBKeychainStoreItemReturnType` desired keychain item return format, this can be combine to each other availabl formats.
/*
// like so:
manager.addReturnType(with: .attributes)
manager.addReturnType(with: .data)
manager.addReturnType(with: .reference)
manager.addReturnType(with: .persistentReference)
*/
// or
manager.addReturnType(with:.attributes)letsearchedResult=try manager.searchItem()print("\nFound Result: \(searchedResult)")iflet searchedResult = searchedResult.item as?[String:Any],let pwdData =searchedResult[TCBKeychainStoreItemAttributeGenericPassword.password.attributeKey]as?Data,let password =String(data: pwdData, encoding:String.Encoding.utf8){print("\n\nFound Password: \(password)")}}catch{iflet error = error as?TCBKeychainStoreStatus{print("\n\n manager: \(error.description!)")}}Sample constructor for deleting an Item from the Keychain.
varmanager=TCBKeychainStoreDeleteManager<TCBKeychainStoreItemAttributeGenericPassword,TCBKeychainStoreItemAttributeSearchQuery>(itemType:.genericPassword)do{try manager.addQuery(for:.account, value:"test_account")letdeleteResult=try manager.deleteItem()print("\n\nDelete Result: \(deleteResult.description!)")}catch{iflet error = error as?TCBKeychainStoreStatus{print("\n\n manager: \(error.description!)")}}Neil Francis Ramirez Hipona, nferocious76@gmail.com
TCBKeychainManager is available under the MIT license. See the LICENSE file for more info.