Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a dictionary for Keychain query with the service name.
iOS Swift
let query: [String: Any] = [kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: [1]]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using kSecClassGenericPassword instead of a string for service.
Using kSecAttrAccount where service is expected.
✗ Incorrect
The service name must be a string identifying your app or service, so "com.example.myapp" is correct.
2fill in blank
mediumComplete the code to convert a password string to Data for Keychain storage.
iOS Swift
let passwordData = password.[1]! Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like utf8Data() or dataValue().
Forgetting to unwrap the optional result.
✗ Incorrect
The correct method to convert a string to Data with UTF-8 encoding is data(using: .utf8).
3fill in blank
hardFix the error in the Keychain add query by completing the code to set the password data.
iOS Swift
query[kSecValueData as String] = [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a String instead of Data to kSecValueData.
Using a variable name that does not exist.
✗ Incorrect
The value for kSecValueData must be Data type, so passwordData is correct.
4fill in blank
hardFill both blanks to complete the code that retrieves password data from Keychain and converts it to String.
iOS Swift
if let data = result as? Data, let password = String([1]: data, [2]: .utf8) { print(password) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' instead of 'init' for String initialization.
Using 'decode' instead of 'encoding' parameter.
✗ Incorrect
To convert Data to String, use String.init(data:encoding:).
5fill in blank
hardFill all three blanks to complete the code that updates an existing Keychain item with new password data.
iOS Swift
let updateQuery: [String: Any] = [kSecClass as String: [1], kSecAttrService as String: service] let attributesToUpdate: [String: Any] = [kSecValueData as String: [2]] let status = SecItemUpdate(updateQuery as CFDictionary, [3] as CFDictionary)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing service instead of attributesToUpdate to SecItemUpdate.
Using wrong constants for kSecClass.
✗ Incorrect
To update Keychain, specify the class, new data, and pass attributesToUpdate to SecItemUpdate.