0

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...

  • Does this answer your question? [SwiftUI iterating through dictionary with ForEach](https://stackoverflow.com/questions/56675532/swiftui-iterating-through-dictionary-with-foreach) – lorem ipsum Feb 27 '22 at 22:51
  • Sorry for the late reply - that link did indeed help, yes! :) I'm finding it difficult to understand how to get the data returned from Keychain.allItems() into something that I could iterate over - I thought a dict would be the way to go, but that doesn't really work. I tried to find inspiration in the demo project (https://github.com/kishikawakatsumi/KeychainAccess/tree/master/Examples/Example-iOS), but I found their way of retrieving the data very weird. – mr-woodapple Mar 05 '22 at 21:43
  • The main question that I can't help myself with right now is how to get information from the Keychain to my dict... I might be missing some crucial understanding here... Help is very much appreciated! :) – mr-woodapple Mar 05 '22 at 21:46
  • Btw: Here's an image of what the Keychain.allItems() returns: https://drive.google.com/file/d/1mv5gbSJ7LbBQYTe_vOCuqn9emOzOtaqI/view?usp=sharing – mr-woodapple Mar 05 '22 at 22:04
  • Its an array of dictionaries so you need 2 loops – lorem ipsum Mar 05 '22 at 22:20
  • Cheers! I don't really know how to start that one and where to save it. I assume having a helper function would be good and not have everything in the actual ForEach loop? – mr-woodapple Mar 05 '22 at 22:59

0 Answers0