Skip to content

Instantly share code, notes, and snippets.

@ksm
Last active October 18, 2017 21:50
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksm/efcc37ba1ce6d5b7472d to your computer and use it in GitHub Desktop.
Save ksm/efcc37ba1ce6d5b7472d to your computer and use it in GitHub Desktop.
Handling SecRequestSharedWebCredential that works with Xcode 7.2 and Swift 2.
SecRequestSharedWebCredential(nil, nil) { credentials, error in
guard error == nil else {
let cocoaError = error! as NSError
let errorIsExpected = cocoaError.domain == NSOSStatusErrorDomain && cocoaError.code == Int(errSecItemNotFound)
if !errorIsExpected {
// Do something with an unexpected error.
}
return
}
guard let credentials = credentials where CFArrayGetCount(credentials) > 0 else {
// Did not find a shared web credential.
return
}
guard CFArrayGetCount(credentials) == 1 else {
// There should be exactly one credential.
return
}
let unsafeCredential = CFArrayGetValueAtIndex(credentials, 0)
let credential = unsafeBitCast(unsafeCredential, CFDictionaryRef.self)
let unsafeAccount = CFDictionaryGetValue(credential, unsafeAddressOf(kSecAttrAccount))
let account = unsafeBitCast(unsafeAccount, CFString.self) as String
let unsafePassword = CFDictionaryGetValue(credential, unsafeAddressOf(kSecSharedPassword))
let password = unsafeBitCast(unsafePassword, CFString.self) as String
print("Found \(account) with \(password).")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment