Skip to content

Instantly share code, notes, and snippets.

@Akii
Created February 14, 2014 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Akii/9001694 to your computer and use it in GitHub Desktop.
Save Akii/9001694 to your computer and use it in GitHub Desktop.
// SERVER_NAME is a defined constant
// Create dictionary of search parameters
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)(kSecClassInternetPassword), kSecClass, SERVER_NAME, kSecAttrServer, kCFBooleanTrue, kSecReturnAttributes, nil];
// Remove any old values from the keychain
OSStatus err = SecItemDelete((__bridge CFDictionaryRef) dict);
// Create dictionary of parameters to add
NSData* passwordData = [credentials.password dataUsingEncoding:NSUTF8StringEncoding];
dict = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)(kSecClassInternetPassword), kSecClass, SERVER_NAME, kSecAttrServer, passwordData, kSecValueData, credentials.username, kSecAttrAccount, nil];
// Try to save to keychain
err = SecItemAdd((__bridge CFDictionaryRef) dict, NULL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment