SecureStorage 是一个围绕 Keychain 的属性包装器,可以轻松访问受保护的数据。
dependencies: [
.package(url: "https://github.com/0xWDG/SecureStorage.git", branch: "main"),
],
targets: [
.target(name: "MyTarget", dependencies: [
.product(name: "SecureStorage", package: "SecureStorage"),
]),
]
https://github.com/0xWDG/SecureStorage
) 并点击 Next。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
想了解更多关于 Swift 的信息吗? 查看我的博客。