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.