Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getAllKeys() method #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions KeychainSwift/KeychainSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,42 @@ open class KeychainSwift {

return nil
}

/**

Return all saved keys in Keychain

- returns: The String array with saved keys

*/
open func getAllKeys() -> [String]? {

var query: [String: Any] = [ kSecClass as String : kSecClassGenericPassword ]
query = addAccessGroupWhenPresent(query)
query = addSynchronizableIfRequired(query, addingItems: false)
query[kSecMatchLimit as String] = kSecMatchLimitAll
query[kSecReturnAttributes as String] = true
lastQueryParameters = query

var result: AnyObject?
var keys = [String]()

lastResultCode = withUnsafeMutablePointer(to: &result) {
SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0))
}

if let items = result as? [[String: AnyObject]] {
for item in items {
if let key = item[String(kSecAttrAccount)] as? String {
keys.append(key)
}
}
}

if lastResultCode == noErr { return keys }

return nil
}

/**

Expand Down