Search code examples
iosxcodeswiftxcode6keychain

What is the more appropriate way to use iOS keychain technology


I'm not fully understand the techniques around iOS keychain technology. What is the more appropriate way to use it?

Suppose that I have the wrapper for the iOS keychain like this -- https://github.com/matthewpalmer/Locksmith and I want to save the user credentials (email and password) for the later use.

Ok, I have the following function from the specified wrapper:

func saveData(data: Dictionary<String, String>, forKey key: String, inService service: String, forUserAccount userAccount: String) -> (NSDictionary?, NSError?) { /* ... */ }

I thought that the best way to call this function is the following:

Locksmith.saveData(self.passwordTextField.text, forKey: "password", inService: "AppName", forUserAccount: self.emailTextField.text)

but I noticed that the first argument is a dictionary. What should be the key for this dictionary? If this is the "password" what should be the argument for the "forKey" parameter then?

And am I right that the argument for the "inService" parameter should be the name of the app?

Thanks in advance.


Solution

  • Think of the keychain itself as a dictionary, where you can store objects based on some key. In this case the key is a combination of a key, a service and a user account. Rather than limiting you to storing a single value per "key", the library allows you to store a dictionary.

    So, in your case you could store a dictionary with a single entry - the user's password for the key "password", but the flexibility is there to store additional entries.

    A common use of Keychain is store a record of in-app purchases. In this case you could store a number of product identifiers and a quantity purchased for each, as an example.