Skip to content

Instantly share code, notes, and snippets.

@scottcarter
Last active June 5, 2018 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottcarter/356a3ac66be048216ac75df42da94959 to your computer and use it in GitHub Desktop.
Save scottcarter/356a3ac66be048216ac75df42da94959 to your computer and use it in GitHub Desktop.
Log or delete keychain items for app
import Foundation
// Log and/or delete keychain items for app.
//
// Credit to:
// https://bootstragram.com/blog/deleting-keychain-items/
//
public class KeychainDebug: NSObject {
@objc public class func iterateKeychainItems(log: Bool, delete: Bool) {
let secItemClasses = [
kSecClassGenericPassword,
kSecClassInternetPassword,
kSecClassCertificate,
kSecClassKey,
kSecClassIdentity
]
if (log) {
for secItemClass in secItemClasses {
let query: [String: Any] = [
kSecReturnAttributes as String: kCFBooleanTrue,
kSecMatchLimit as String: kSecMatchLimitAll,
kSecClass as String: secItemClass
]
var result: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &result)
if status == noErr {
print(result as Any)
}
}
print("AppUsageMetadata.iterateKeychainItems ended.")
}
if (delete) {
for secItemClass in secItemClasses {
let dictionary = [kSecClass as String:secItemClass]
SecItemDelete(dictionary as CFDictionary)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment