0
0
iOS Swiftmobile~20 mins

Keychain for secure storage in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Keychain Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
1:30remaining
How does Keychain store data securely?

Which statement best describes how the iOS Keychain stores data?

AIt saves data as plain text files inside the app's document directory.
BIt stores data in UserDefaults but with a password lock.
CIt encrypts data and stores it in a secure system area accessible only to the app or authorized apps.
DIt sends data to iCloud and retrieves it when needed.
Attempts:
2 left
💡 Hint

Think about where sensitive data like passwords should be kept on iOS devices.

📝 Syntax
intermediate
2:00remaining
What is the correct way to add a password to Keychain in Swift?

Given the following Swift code snippet, which option correctly adds a password string to the Keychain?

let password = "mypassword123"
let account = "user@example.com"
let service = "com.example.app"
A
let query = [kSecClass: kSecClassGenericPassword, kSecAttrAccount: account, kSecAttrService: service, kSecValueData: password.data(using: .utf16)!]
SecItemAdd(query as CFDictionary, nil)
B
let query: [String: Any] = [kSecClass: kSecClassGenericPassword, kSecAttrAccount: account, kSecAttrService: service, kSecValueData: password]
SecItemAdd(query, nil)
C
let query = ["class": "genericPassword", "account": account, "service": service, "value": password]
SecItemAdd(query, nil)
D
let query: [String: Any] = [kSecClass as String: kSecClassGenericPassword, kSecAttrAccount as String: account, kSecAttrService as String: service, kSecValueData as String: password.data(using: .utf8)!]
SecItemAdd(query as CFDictionary, nil)
Attempts:
2 left
💡 Hint

Remember the password must be converted to Data using UTF-8 encoding before storing.

lifecycle
advanced
1:30remaining
When is Keychain data accessible to an app?

Consider an app that stores a password in Keychain with kSecAttrAccessibleAfterFirstUnlock. When can the app access this data?

AOnly when the device is unlocked for the first time after reboot, and afterwards even if locked.
BOnly while the app is running in the foreground.
COnly when the device is unlocked and the app is active.
DAt any time, even if the device is locked or rebooted.
Attempts:
2 left
💡 Hint

Think about the meaning of AfterFirstUnlock accessibility option.

🔧 Debug
advanced
2:00remaining
Why does this Keychain query fail to find stored data?

Given this Swift code to retrieve a password from Keychain, why does it fail to find the stored password?

let query: [String: Any] = [kSecClass as String: kSecClassGenericPassword, kSecAttrAccount as String: "user@example.com", kSecReturnData as String: true]
var item: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &item)
AThe <code>kSecReturnData</code> key should be false to retrieve the password.
BThe query is missing the <code>kSecAttrService</code> key, so it does not match the stored item.
CThe <code>item</code> variable is not initialized to nil before the call.
DThe <code>kSecClass</code> value should be <code>kSecClassInternetPassword</code> instead.
Attempts:
2 left
💡 Hint

Think about what keys were used when storing the password.

🧠 Conceptual
expert
1:30remaining
What is the main advantage of using Keychain over UserDefaults for sensitive data?

Why should developers prefer Keychain instead of UserDefaults to store passwords or tokens?

AKeychain encrypts data and restricts access based on device security, while UserDefaults stores data unencrypted and accessible to anyone with device access.
BUserDefaults automatically syncs data across devices, but Keychain does not support syncing.
CKeychain is faster to read and write data than UserDefaults.
DUserDefaults requires explicit user permission to store data, Keychain does not.
Attempts:
2 left
💡 Hint

Consider the security and privacy differences between the two storage methods.