This library allows you simply and fast deal with Keychain.
Add import KeychainManager
to your source code.
Since the library uses Generic types, you can pass as a parameter any values that are signed under the Codable protocol.
All functions above return Bool
value as a result.
- String:
KeychainManager.save(value: "SuperSecretInfo", key: "SomeSecretKey")
- Int:
KeychainManager.save(value: 2021, key: "Year")
- Bool:
KeychainManager.save(value: true, key: "Status")
- Custom Types:
class BearerResponse: Codable {
var accesToken: String?
var refreshToken: String?
}
let bearer = BearerResponse()
bearer.accesToken = "some-acces-token"
bearer.refreshToken = "some-refresh-token"
KeychainManager.save(value: bearer, key: "tokenData")
KeychainManager.save(value: "Info", key: "key") {
// succes
} failure: status in
// failure
}
Library provides two types of get
functions.
In this case, you need to cast the type of the function result yourself.
let someText = KeychainManager.get(forKey: "SomeSecretKey") as? String
or
KeychainManager.get(forKey: "SomeSecretKey") { (obj) in
// succes, type of obj == Any?
} failure: {
// failure
}
And the second function accepts the type of object in parameters.
let someInt = KeychainManager.get(forKey: "Year", asType: Int.self) // return Int?
or
KeychainManager.get(forKey: "Year ", asType: Int.self) { (obj) in
// succes , type of obj == Int?
} failure: {
// failure
}
Returns Bool
as result.
KeychainManager.update(value: "2077", forKey: "Year")
or
KeychainManager.update(value: "2077", forKey: "Year") {
// succes
} failure: { status in
// failure
}
KeychainManager.erase(key: "Year")
or
KeychainManager.erase(key: "Year") {
// succes
} failure: { status in
// failure
}
KeychainManager.eraseAll()
You can use Swift Package Manager to install KeychainManager using Xcode:
- Open your project in Xcode
- Click "File" -> "Swift Packages" -> "Add Package Dependency..."
- Paste the following URL: https://github.com/Echobird/KeychainManager
- Click "Next" -> "Next" -> "Finish"
- Deal with Advanced Mode
- Allow Installation via CocoaPods, Carthage
- Some small refactor