From 97dc7042b4594045d1a33808b229530b1d1e9eb3 Mon Sep 17 00:00:00 2001 From: "kirill.filippov" Date: Tue, 13 Sep 2016 09:12:23 +0200 Subject: [PATCH 1/2] Added getAllKeys() method --- KeychainSwift/KeychainSwift.swift | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/KeychainSwift/KeychainSwift.swift b/KeychainSwift/KeychainSwift.swift index 8103faa..ad09b19 100644 --- a/KeychainSwift/KeychainSwift.swift +++ b/KeychainSwift/KeychainSwift.swift @@ -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 keys + } /** From 078f94b24af505da31d0787d117fc1c923e8ea15 Mon Sep 17 00:00:00 2001 From: "kirill.filippov" Date: Tue, 13 Sep 2016 12:21:49 +0200 Subject: [PATCH 2/2] Return nil if there is no saved items --- KeychainSwift/KeychainSwift.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/KeychainSwift/KeychainSwift.swift b/KeychainSwift/KeychainSwift.swift index ad09b19..a1d6fbd 100644 --- a/KeychainSwift/KeychainSwift.swift +++ b/KeychainSwift/KeychainSwift.swift @@ -212,7 +212,7 @@ open class KeychainSwift { if lastResultCode == noErr { return keys } - return keys + return nil } /**