We need only one thing in order to delete an item from the Keychain: a
CFDictionary with attributes describing the items to be deleted. Any items that match the query dictionary will be deleted permanently, so if you are only intending to delete a single item be sure to be specific with your query. As always, we can use an
NSDictionary in Objective-C or in Swift we can use a
Dictionary and then cast to
A query dictionary, in this context exclusively includes a class key to describe what the item is and attributes to describe information about the item. Inclusion of search restrictions such as
kSecMatchCaseInsensitive is not allowed.
var dict = [String : AnyObject]() dict[kSecClass as String] = kSecClassGenericPassword // Label dict[kSecAttrLabel as String] = "com.me.myapp.myaccountpassword" as CFString // Username dict[kSecAttrAccount as String] = "My Name" as CFString
And now we can simply remove it:
let status = SecItemDelete(dict as CFDictionary)
SecItemDelete returns an
OSStatus. Result codes are described here.