Skip to content

Instantly share code, notes, and snippets.

@fitomad
Created April 8, 2019 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitomad/9b291baf4f8e7579c2f9405bb6e49163 to your computer and use it in GitHub Desktop.
Save fitomad/9b291baf4f8e7579c2f9405bb6e49163 to your computer and use it in GitHub Desktop.
// Ejecutamos la búqueda
let status = SecItemCopyMatching(query as CFDictionary, &item)
// Comprobamos si no aparece el elemento...
if status == errSecItemNotFound
{
return Result.failure(KeychainError.passwordNotFound)
}
// ...y en caso de encontrarlos lo procesamos
if status == errSecSuccess
{
guard let existingItem = item as? [String : Any],
let passwordData = existingItem[kSecValueData as String] as? Data,
let password = String(data: passwordData, encoding: .utf8),
let account = existingItem[kSecAttrAccount as String] as? String
else
{
return Result.failure(KeychainError.malformedData)
}
let user = User(named: account, withPassword: password)
return Result.success(user)
}
else
{
return Result.failure(KeychainError.unknown(status: status))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment