Created
April 9, 2014 16:12
-
-
Save btjones/10287581 to your computer and use it in GitHub Desktop.
An updated version of SimpleKeychain (original: http://stackoverflow.com/a/5251820/334725) that supports accounts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@class SimpleKeychainUserPass; | |
@interface SimpleKeychain : NSObject | |
+ (void)save:(NSString *)service account:(NSString *)account data:(id)data; | |
+ (id)load:(NSString *)service account:(NSString *)account; | |
+ (void)delete:(NSString *)service account:(NSString *)account; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "SimpleKeychain.h" | |
@implementation SimpleKeychain | |
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service account:(NSString *)account { | |
return [NSMutableDictionary dictionaryWithObjectsAndKeys: | |
(id)kSecClassGenericPassword, (id)kSecClass, | |
service, (id)kSecAttrService, | |
account, (id)kSecAttrAccount, | |
(id)kSecAttrAccessibleAfterFirstUnlock, (id)kSecAttrAccessible, | |
nil]; | |
} | |
+ (void)save:(NSString *)service account:(NSString *)account data:(id)data { | |
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service account:account]; | |
SecItemDelete((CFDictionaryRef)keychainQuery); | |
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData]; | |
SecItemAdd((CFDictionaryRef)keychainQuery, NULL); | |
} | |
+ (id)load:(NSString *)service account:(NSString *)account { | |
id ret = nil; | |
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service account:account]; | |
[keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData]; | |
[keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit]; | |
CFDataRef keyData = NULL; | |
if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) { | |
@try { | |
ret = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)keyData]; | |
} | |
@catch (NSException *e) { | |
NSLog(@"Unarchive of %@ failed: %@", service, e); | |
} | |
@finally {} | |
} | |
if (keyData) CFRelease(keyData); | |
return ret; | |
} | |
+ (void)delete:(NSString *)service account:(NSString *)account { | |
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service account:account]; | |
SecItemDelete((CFDictionaryRef)keychainQuery); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This solution is no good for arc projects. See this Stack Overflow post for an arc'ified solution:
http://stackoverflow.com/a/20172496/1071320