8.5. Deleting Exiting Values in the Keychain
Problem
You want to delete a keychain item.
Solution
Use the SecItemDelete
function.
Discussion
In Recipe 8.2, we learned
how to store values in the keychain. In order to delete those values,
you will need to use the SecItemDelete function. This function takes
only one parameter: a dictionary of type CFDictionaryRef. You can take a normal
dictionary and bridge-convert it to an instance of CFDictionaryRef, as we have done in other
recipes in this chapter. The dictionary that you’ll pass to this method
has to contain the following keys:
kSecClassThe type of item that you want to delete. For instance
kSecClassGenericPassword.kSecAttrServiceThe service that this item is hooked to. When you stored the item, you chose the service for it, so you’ll need to provide the same service here. For instance, in previous examples, we set the value of this key to our app’s bundle identifier. If that’s what you did as well, simply provide your app’s bundle identifier for the value of this key.
kSecAttrAccountThe value of this key is the key that has to be deleted.
Assuming you have followed Recipe 8.2, the keychain now has a
generic password (kSecClassGenericPassword) with the service
name (kSecAttrService) equal to our
app’s bundle ID and the key (kSecAttrAccount) equal to Full
Name. To delete this key, here is what you’ll have to
do:
#import "AppDelegate.h"#import <Security/Security.h>@implementationAppDelegate-(BOOL)application:(UIApplication*)application ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access