Permalink
Browse files

Setting a key to nil removes the key from the keychain.

  • Loading branch information...
1 parent 1bf0ef9 commit eabd2a7f0698879b1a15d3d2b97fad2a8702c0df @granoff committed Apr 22, 2012
Showing with 13 additions and 4 deletions.
  1. +13 −4 Lockbox.m
View
@@ -33,12 +33,21 @@ +(NSMutableDictionary *)_query
+(BOOL)setObject:(NSString *)obj forKey:(NSString *)key
{
+ OSStatus status;
+
+ // If the object is nil, delete the item
+ if (!obj) {
+ NSMutableDictionary *query = [Lockbox _query];
+ [query setObject:key forKey:(id)kSecAttrService];
+ status = SecItemDelete((CFDictionaryRef)query);
+ return (status == errSecSuccess);
+ }
+
NSMutableDictionary *dict = [Lockbox _service];
[dict setObject: key forKey: (id) kSecAttrService];
[dict setObject: [obj dataUsingEncoding:NSUTF8StringEncoding] forKey: (id) kSecValueData];
- // Should really check the status here, and the method should return BOOL
- OSStatus status = SecItemAdd ((CFDictionaryRef) dict, NULL);
+ status = SecItemAdd ((CFDictionaryRef) dict, NULL);
if (status == errSecDuplicateItem) {
NSMutableDictionary *query = [Lockbox _query];
[query setObject:key forKey:(id)kSecAttrService];
@@ -47,7 +56,7 @@ +(BOOL)setObject:(NSString *)obj forKey:(NSString *)key
status = SecItemAdd((CFDictionaryRef) dict, NULL);
}
if (status != errSecSuccess)
- NSLog(@"SecItemAdd failed: %ld", status);
+ NSLog(@"SecItemAdd failed for key %@: %ld", key, status);
return (status == errSecSuccess);
}
@@ -60,7 +69,7 @@ +(NSString *)objectForKey:(NSString *)key
NSData *data = nil;
OSStatus status = SecItemCopyMatching ( (CFDictionaryRef) query, (CFTypeRef*) &data );
if (status != errSecSuccess)
- NSLog(@"SecItemCopyMatching failed: %ld", status);
+ NSLog(@"SecItemCopyMatching failed for key %@: %ld", key, status);
if (!data)
return nil;

0 comments on commit eabd2a7

Please sign in to comment.