O'Reilly logo

iOS 7 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

kSecClass

The type of item that you want to delete. For instance kSecClassGenericPassword.

kSecAttrService

The 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.

kSecAttrAccount

The 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>

@implementation AppDelegate

- (BOOL)            application:(UIApplication *)application ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required