From the course: iOS Development: Security

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Adding a value to the Keychain

Adding a value to the Keychain - iOS Tutorial

From the course: iOS Development: Security

Start my 1-month free trial

Adding a value to the Keychain

- [Instructor] Next, we implement the method to store a string securely in the keychain. I'm going to call it set string for key. Let me make some space here so that we can see what we're doing. This method is not private because it's part of the public API of this facade, so it's a public method, and I call it, set, the first parameter is called string, just type string, the second one is the key, also of type string. Now, there may be various issues while accessing that keychain, so I make this a throwable method. First, we validate the input parameters. I use the guard statement, and first we check if the string is empty, and then the key, as well. The guard should fail if any of the input arguments is empty so I'm going to print an error message to the console, say, "Can't add an empty string to the keychain." We should also throw an error, so let's declare a custom error type. I scroll up and define the keychain for said error, enum, that conforms to the error protocol. It should…

Contents