strong textI'm new to Swift and been trying to develope a password manager as a starter project (not sure if that's a bit too much, but hey here we are). ^^
I wanted to use Apples Keychain to securely store password (to be exact: email, password, comment, website, and an Array containing entries the user can modify with a title and a text element). To make life a bit easier, I'm using this wrapper: https://github.com/kishikawakatsumi/KeychainAccess
The thing I don't understand is how to iterate over each of the items currently stored in my Keychain. There's a test project in the repo, that didn't helped me at all.
Here's my attempt to iterate over all Keychain entries with a ForEach loop:
import SwiftUI
import KeychainAccess
struct PasswordsView: View {
// Makes the passwordEntry available as an object
@EnvironmentObject var entryData: EntryData
// Used to create a new password entry
@State private var isAddingNewEntry = false
@State private var newEntry = PasswordEntry()
// NEW get the keychain entries
let passwordItems = Keychain().allItems()
var body: some View {
NavigationView {
// List with keychain entries
List {
Section(
header: Text("entries")
.font(.callout)
.foregroundColor(.secondary)
.fontWeight(.bold)
) {
ForEach(passwordItems) { passwordItems in
HStack {
Text((passwordItems["key"] as? String)! ?? <#default value#>)
Text((passwordItems["value"] as? String)! ?? <#default value#>)
}
}
}
}
// Navigationbar with title & plus icon to add new entries
.navigationTitle("Passwords")
.toolbar{
ToolbarItem {
// Add the plus icon to the toolbar
Button {
newEntry = PasswordEntry()
// Set isAddingNewEntry to true do display the sheet
isAddingNewEntry = true
} label: {
Image(systemName: "plus")
}
}
}
.sheet(isPresented: $isAddingNewEntry) {
NavigationView {
EntryEditor(passwordEntry: newEntry, isNew: true)
}
}
}
}
}
What is wrong and/or missing to make this work? I'm getting a "Referencing initializer 'init(_:content:)' on 'ForEach' requires that '[String : Any]' conform to 'Identifiable'" error on the ForEach loop at the moment.
And what I like to know: What exactly is the Keychain returning when accessing all items? I thought it was a dict, but maybe I'm wrong?
P.S.: Feel free to let me know if the keychain is the absolute wrong place to store credentials AND "metadata"... I thought about SQLite, but not beeing able to encrypt things seemed like a major disadvantange for my purpose...