SecureStorage is a property wrapper around the keychain to easily access your protected data.
- Swift 5.9+ (Xcode 15+)
- iOS 13+, macOS 10.15+
dependencies: [
.package(url: "https://github.com/0xWDG/SecureStorage.git", branch: "main"),
],
targets: [
.target(name: "MyTarget", dependencies: [
.product(name: "SecureStorage", package: "SecureStorage"),
]),
]
- In Xcode, open your project and navigate to File โ Swift Packages โ Add Package Dependency...
- Paste the repository URL (
https://github.com/0xWDG/SecureStorage
) and click Next. - Click Finish.
import SwiftUI
import SecureStorage
struct ContentView: View {
// For this example, we directly bind the username & password.
// This is not smart to do, because you'll overwrite the values as you type.
@SecureStorage("username")
var username: String?
@SecureStorage("password")
var password: String?
var body: some View {
VStack {
Text("Please login")
TextField("Username", text: $username ?? "")
SecureField("Password", text: $password ?? "")
Button("Login") {
print("Login", username, password)
}
Button("Delete username") {
SecureStorage("username").delete()
}
Button("Delete password") {
SecureStorage("password").delete()
}
Button("Delete username") {
SecureStorage("*").deleteAll()
}
}
}
}
๐ฆ @0xWDG ๐ mastodon.social/@0xWDG ๐ฆ @0xWDG ๐งต @0xWDG ๐ wesleydegroot.nl ๐ค Discord
Interested learning more about Swift? Check out my blog.