From the course: iOS Development: Security

Unlock the full course today

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

Restore values from the Keychain

Restore values from the Keychain - iOS Tutorial

From the course: iOS Development: Security

Start my 1-month free trial

Restore values from the Keychain

- [Instructor] Now let's use this new feature in the ViewController. Let me switch to the ViewController. In the viewDidLoad method I retrieve the strings we persisted in the Keychain. So let's crawl down to the viewDidLoad method. And I'm going to add the required code. I use the guard statement to check if the string forKey method calls were successful. Guard let username equals I'm going to use try question mark which ignores if an error was thrown and return is nil. Try keychain .string forKey. And the key for the username was username as we can see here. So let's copy and paste it from that part. And also let's fetch the password equals try question mark keychain.string forKey and I'll be using the key for the password. If any of these calls fails I print a warning message and return. Print. Could not retrieve credentials from Keychain. And let's return. Now if the call succeeds we set the username and the password for the corresponding task fields. Let' scroll up a bit so we can…

Contents