Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dariowskii/keychain_wrapper

a simple KeychainWrapper to get, set, remove any Codable object from Keychain
https://github.com/dariowskii/keychain_wrapper

codable ios keychain keychain-wrapper swift swiftui wrapper

Last synced: 27 days ago
JSON representation

a simple KeychainWrapper to get, set, remove any Codable object from Keychain

Awesome Lists containing this project

README

        

# keychain_wrapper

A simple KeychainWrapper to get, set, remove any `Codable` object from Keychain!

## Examples

You can `get` a value in this way:
```swift
do {
let myPassword = try KeychainWrapper.shared.get(forKey: "myPasswordKey", expecting: String.self)
} catch {
// handle error
}
// ...
// or without handling error
let myPassword: String? = KeychainWrapper.shared.get(forKey: "myPasswordKey")
```

You can `set` a new value for a specific key in this way:
```swift
let myObject: Codable = MyObject()

do {
try KeychainWrapper.shared.set(value: myObject, forKey: "myObjectKey")
} catch {
// handle error
}
```

You can `remove` a value for a specific key in this way:
```swift
do {
try KeychainWrapper.shared.remove(forKey: "myObjectKey")
} catch {
// handle error
}
```

You can remove all the stored values in this way:
```swift
do {
try KeychainWrapper.shared.deleteAll()
} catch {
// handle error
}
```

## KeychainItem

You can also use the `KeychainItem` **propertyWrapper** to use the KeychainWrapper quickly and intuitively, in this way:
```swift
class SharedData {
static let shared = SharedData()
private init() {}

@KeychainItem(key: .accessToken)
var accessToken: String?
}

// You can use it like normal variable
let myAccessToken = SharedData.shared.accessToken
SharedData.shared.accessToken = "mySecretToken"
```

---

Made with ❤️ from [dariowskii](https://www.linkedin.com/in/dario-varriale/)