Last active
September 17, 2015 14:30
-
-
Save baileysh9/e4661d54321aafea3f0d to your computer and use it in GitHub Desktop.
Load Login from Keychain Swift
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
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