0
struct UserData {
     var userName: String? = nil
     var password: String? = nil
     var phone: String? = nil
     var email: String? = nil
     var image: UIImage? = nil
     var categoryName: String? = nil
}

I need to save and retrieve this data into keychain.How can save and retrieve in proper way and also while i retrieve data i need to filter this data with category name

  • 3
    What did you try? – Mojtaba Hosseini Aug 23 '21 at 12:42
  • It would be easier to encrypt and save it in your application support directory. Or just save each property by itself considering it will always be just a single user. I would save the image in your app support directory or documents directory. keychain is meant to store sensitive information. Not images. – Leo Dabus Aug 23 '21 at 18:39
  • 2
    As already mentioned. This is not a code writing service. Please edit your question post what you have tried and the issues you are facing. – Leo Dabus Aug 23 '21 at 18:43

1 Answers1

0

You can make UserData conform to Codable by implementing custom encoder and decoder.

All the fields except image are codable by default. You can store the image as string by exporting it's data as base64String.

Then you can transform the struct into data with JSONEncoder. And use it's base64Encoded representation to store in keychain.

But this approach has a serious issue: keychain is not good for storing large amounts of data. It is meant to be used to store passwords and stuff.

So I'd make some sort of service that would manage UserData objects. It would store sensitive data like password in the keychain, and other not-so-sensitive fields as files on a disk.

  • func setupView(){ let credential = UserData(accountName: accountName.text!, userName:userName.text!,password: password.text!,accountURL: accountURL.text!,descriptionText: descriptionText.text,categoryImage:imageLabel.text!, categoryName: categoryLabel.text!) do { try Keychain.default.store(credential) } catch let error { print(error) } } – islahulislam.ca Aug 25 '21 at 09:36