Created
January 12, 2011 08:53
-
-
Save ndfred/775896 to your computer and use it in GitHub Desktop.
Saving a login and a password to the iOS keychain
This file contains 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
- (void)setCredentialsFromKeychain { | |
NSMutableDictionary *query = [NSMutableDictionary dictionary]; | |
NSDictionary *result = nil; | |
[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass]; | |
[query setObject:kLCSessionManagerKeychainSubscriptionServiceKey forKey:(id)kSecAttrService]; | |
[query setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnAttributes]; | |
[query setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData]; | |
if (SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&result) == errSecSuccess) { | |
self.username = [result objectForKey:(id)kSecAttrAccount]; | |
self.password = [[[NSString alloc] initWithData:[result objectForKey:(id)kSecValueData] encoding:NSUTF8StringEncoding] autorelease]; | |
NSLog(@"Got credentials from keychain: %@ / %@", self.username, self.password); | |
} | |
} | |
- (void)_saveCredentialsToKeychain { | |
NSMutableDictionary *query = [NSMutableDictionary dictionary]; | |
[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass]; | |
[query setObject:kLCSessionManagerKeychainSubscriptionServiceKey forKey:(id)kSecAttrService]; | |
if (self.username == nil || self.password == nil) { | |
self.username = nil; | |
self.password = nil; | |
if (SecItemCopyMatching((CFDictionaryRef)query, NULL) == errSecSuccess) { | |
NSLog(@"Removing credentials from keychain"); | |
OSStatus deleteStatus = SecItemDelete((CFDictionaryRef)query); | |
NSLog(@"Result: %d", deleteStatus); | |
} else { | |
NSLog(@"No credentials to remove from keychain"); | |
} | |
} else { | |
NSMutableDictionary *credentials = [NSMutableDictionary dictionary]; | |
[credentials setObject:(id)self.username forKey:(id)kSecAttrAccount]; | |
[credentials setObject:(id)[self.password dataUsingEncoding:NSUTF8StringEncoding] forKey:(id)kSecValueData]; | |
if (SecItemCopyMatching((CFDictionaryRef)query, NULL) == errSecSuccess) { | |
NSLog(@"Updating credentials in keychain to: %@ / %@", self.username, self.password); | |
OSStatus updateStatus = SecItemUpdate((CFDictionaryRef)query, (CFDictionaryRef)credentials); | |
NSLog(@"Result: %d", updateStatus); | |
} else { | |
[credentials setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass]; | |
[credentials setObject:kLCSessionManagerKeychainSubscriptionServiceKey forKey:(id)kSecAttrService]; | |
NSLog(@"Saving credentials to keychain: %@ / %@", self.username, self.password); | |
OSStatus addStatus = SecItemAdd((CFDictionaryRef)credentials, NULL); | |
NSLog(@"Result: %d", addStatus); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment