0
0
iOS Swiftmobile~10 mins

Keychain for secure storage in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A"com.example.myapp"
BkSecClassGenericPassword
C"password"
DkSecAttrAccount
Attempts:
3 left
💡 Hint
Common Mistakes
Using kSecClassGenericPassword instead of a string for service.
Using kSecAttrAccount where service is expected.
2fill in blank
medium

Complete 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'
Adata()
BdataValue()
Cutf8Data()
Ddata(using: .utf8)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like utf8Data() or dataValue().
Forgetting to unwrap the optional result.
3fill in blank
hard

Fix 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'
ApasswordData
Bpassword
CpasswordString
Dpassword.data
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a String instead of Data to kSecValueData.
Using a variable name that does not exist.
4fill in blank
hard

Fill 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'
Ainit
Bdata
Cencoding
Ddecode
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' instead of 'init' for String initialization.
Using 'decode' instead of 'encoding' parameter.
5fill in blank
hard

Fill 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'
AkSecClassGenericPassword
BpasswordData
CattributesToUpdate
Dservice
Attempts:
3 left
💡 Hint
Common Mistakes
Passing service instead of attributesToUpdate to SecItemUpdate.
Using wrong constants for kSecClass.