Skip to content

Instantly share code, notes, and snippets.

@baileysh9
Last active September 17, 2015 14:30
Show Gist options
  • Save baileysh9/e4661d54321aafea3f0d to your computer and use it in GitHub Desktop.
Save baileysh9/e4661d54321aafea3f0d to your computer and use it in GitHub Desktop.
Load Login from Keychain Swift
func load(service: NSString, userName:String) -> NSString?
{
//Create the query keychain
let keychainQuery: NSMutableDictionary = NSMutableDictionary(objects: [kSecClassGenericPasswordValue, service, userName, kCFBooleanTrue, kSecMatchLimitOneValue], forKeys: [kSecClassValue, kSecAttrServiceValue, kSecAttrAccountValue, kSecReturnDataValue, kSecMatchLimitValue])
var dataTypeRef : Unmanaged<AnyObject>?
// Search for the keychain items
let status: OSStatus = withUnsafeMutablePointer(&dataTypeRef) { SecItemCopyMatching(keychainQuery as CFDictionaryRef, UnsafeMutablePointer($0)) }
print(status)
let opaque = dataTypeRef?.toOpaque()
var contentsOfKeychain: NSString?
if let op = opaque
{
let retrievedData = Unmanaged<NSData>.fromOpaque(op).takeUnretainedValue()
// Convert the data retrieved from the keychain into a string
contentsOfKeychain = NSString(data: retrievedData, encoding: NSUTF8StringEncoding)
}
return contentsOfKeychain
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment