Swift Property Wrapper for Keychain Items
A simple Swift property wrapper for storing data in the keychain. It supports:
Codable
.Declare a property in one of your types:
@KeychainStored(service: "com.example.my-service") var myString: String
Then just use it like any other property:
// To delete
myString = nil
// To set
myString = "Some value"
// To get
if let myString = myString {
// π
}
Note that wrapped values are always accessed as optionals.
Thatβs all there is to it!
By default, the property wrapper will use a JSONEncoder()
and JSONDecoder()
to encode values, but you can use a custom encoder and/or decoder.
The coders must implement the TopLevelEncoder
and TopLevelDecoder
protocols, where the input and output types are required to be Data
. JSONEncoder
, JSONDecoder
, PropertyListEncoder
, and PropertyListDecoder
work out of the box.
Note that the coders are not used if the type of the wrapped value is String
. Strings are always stored directly with UTF-8 encoding.
To use your custom coders:
@KeychainStored(service: "...", encoder: PropertyListEncoder(), decoder: PropertyListDecoder()) var myVar: MyStruct
By default, errors are logged using print
. The initialiser of the @KeychainStored
property wrapper accepts a logger
closure that you can use to customise logging behaviour.